jQuery 将多个表单序列化在一起?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9280720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:35:08  来源:igfitidea点击:

Serialize multiple forms together?

jqueryserializationjsonobject

提问by arbme

Can you serialize multiple forms into one so that only one post or ajax request is made? I have searched around and it is all for submiting each form separently via post/ajax.

您可以将多个表单序列化为一个,以便只发出一个 post 或 ajax 请求吗?我已经四处搜索,这一切都是为了通过 post/ajax 单独提交每个表单。

采纳答案by Jacob Mattison

When you use the jQuery serialize()function, it simply turns your form into a string in the format a=1&b=2&c=3. So you can certainly apply this function to two forms and concatenate the result, with an &between them, and use the result in your ajax call. You'd want some checks to make sure neither string is empty when you do the concatenation.

当您使用 jQueryserialize()函数时,它只是将您的表单转换为格式为 的字符串a=1&b=2&c=3。因此,您当然可以将此函数应用于两种形式并将结果连接&起来,在它们之间使用 ,然后在您的 ajax 调用中使用结果。您需要进行一些检查以确保在进行连接时两个字符串都不为空。

回答by 472084

If you run $('form').serialize()on a page with multiple forms, it will correctly serialize allthe forms into one string.

如果您$('form').serialize()在具有多个表单的页面上运行,它将正确地将所有表单序列化为一个字符串。

To include only certain forms, use $('#form1, #form2').serialize()

要仅包含某些形式,请使用 $('#form1, #form2').serialize()

回答by Vins

I like the answer from Jleagleabove.

我喜欢上面Jleagle的回答。

if you are more specific about the forms , use

如果您对表格更具体,请使用

$('#detailsform,#levelForm').serialize();

the above line will return a string value. like customId=08071992&cort=01&empId=7777

上面的行将返回一个字符串值。喜欢customId=08071992&cort=01&empId=7777

you can avoid unwanted fields by adding the attribute disabled="disabled"to the input fields or in other words "disabled" fields won't be get serialized.

您可以通过将属性添加disabled="disabled"到输入字段来避免不需要的字段,或者换句话说,“禁用”字段不会被序列化。