jquery serialization Form serialize () Serializearray ()

Source: Internet
Author: User

1. Serialize () method

Description: Serializes the contents of the form to a string that is used for AJAX requests.

Format: var data = $ ("form"). Serialize ();

2.serializeArray () method

Description: Serializes a FORM element (similar to the '. Serialize () ' method) to return the JSON data structure.

Note that this method returns a JSON object rather than a JSON string. You need to use plug-ins or <a> third-party libraries </a> for string operations.

Format: var jsondata = $ ("form"). Serializearray ();

Both of these methods can set the data parameter to $ ("form") when submitting form data using AJAX. Serialize () or $ ("form"). Serializearray ().


Demo

<form id= "MyForm" ><table><tr><td> name:</td><td> <input type= "text" name= "name"/> </ td></tr><tr><td> Sex: </td><td><input type= "Radio" name= "Sex" value= "1" > Male < Input type= "Radio" name= "Sex" value= "0" > Women </td></tr><tr><td> Age:</td><td> <select name= "Age" ><option value= ">20</option><option value=" >21</option>< Option value= ">22</option></select></td></tr><tr><td colspan=" 2 ">< Input type= "button" id= "ajaxbtn" value= "commit"/></TD></TR></TABLE></FORM> 
$ (function () {   $ ("#ajaxBtn"). Click (function () {var params1 = $ ("#myform"). Serialize (); var params2 = $ ("#myform"). Serializearray (); Console.log (PARAMS1);  Name=zhangsan&sex=1&age=20console.log (PARAMS2);  [Object, Object, Object]$.ajax ({type: "POST", url: "Registeraction.action", Data:params1,success:function (msg) {Ale RT ("Success:" + msg);})})   
the difference between the two methods can be seen from the


Itmyhome

Source: http://blog.csdn.net/itmyhome1990/article/details/41866265



jquery serialization Form serialize () Serializearray ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.