Serialize multiple forms together?

戏子无情 提交于 2019-11-26 19:23:12

问题


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.


回答1:


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.




回答2:


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

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




回答3:


I like the answer from Jleagle above.

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

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.



来源:https://stackoverflow.com/questions/9280720/serialize-multiple-forms-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!