简体   繁体   English

使用JavaScript捕获整个表单输入数据并通过AJAX发送

[英]Capturing entire form input data with JavaScript and sending it over AJAX

I have a dynamically generated form that I would like to submit using Ajax. 我有一个动态生成的表单,我想使用Ajax提交。 The form contains some input fields and some checkboxes - so the total number of checkboxes that will be submitted is obviously unknown at the start. 表单包含一些输入字段和一些复选框 - 因此,提交的复选框总数在开始时显然是未知的。 By "dynamically generated" I mean the contents and the id's of the form elements are pulled from a DB and again, I never know how many there will be in advance. 通过“动态生成”,我的意思是表单元素的内容和id从数据库中提取出来,我再也不会知道预先有多少。

From what I understand, to submit it with Ajax I need to execute something like this in my javascript function: 根据我的理解,使用Ajax提交它我需要在我的javascript函数中执行类似的操作:

xmlhttp.open("GET","/incl/myfile.php?var[]=a&var[]=b&...", true);

My problem is generating those "var[]=a&var[]=b&..." lists. 我的问题是生成那些“var [] = a&var [] = b&...”列表。 Particularly, I have a hard time understanding how to generate it for those checkboxes, which may or may not be checked. 特别是,我很难理解如何为那些复选框生成它,可能会或可能不会检查。

Is there something that will capture the entire content of the form input fields and format it into a nice GET url query? 有没有什么能够捕获表单输入字段的整个内容并将其格式化为一个不错的GET URL查询? A plugin perhaps? 也许一个插件? Something in JQuery maybe? 可能是JQuery中的东西?

Yes, take a look at jQuery's serialize method. 是的,看看jQuery的序列化方法。 http://api.jquery.com/serialize/ http://api.jquery.com/serialize/

你想要jQuery的.serialize()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM