1.serialize () method
Format: var data = $ ("form"). Serialize ();
Function: Serializes the contents of the form into a single string.
This way, when Ajax submits the form data, it is not necessary to enumerate each parameter. Simply set the data parameter to $ ("form"). Serialize ().
Like other methods in jquery, the Serialize () method acts on a jquery object, which serializes the contents of the DOM elements into strings for Ajax requests. By using the Serialize () method, you can submit all fields of this page with the following code:
1 $ ("#send"). Click (function() {23 function(data, Textstatus)45 $ ("#resText"). HTML (data); 6 7 }); 8 9 });
When you click the Submit button, all form elements that belong to Form1 can be submitted to the background, and even if you add fields to the form, the script is still available and does not require any extra work.
In string mode, you need to be aware of the character encoding (Chinese problem), if you do not want coding to cause trouble, you can use the serialize () method, it will be automatically encoded.
Because the serialize () method is used for jquery objects, it can be used not only by the form, but also by the elements selected by other selectors, such as the following jquery code:
$ (": Checkbox,:radio"). Serialize ();
Serializes the value of a check box and a radio box to a string, only the selected value is serialized.
In jquery There is also a method similar to serialize ()--serializearray (), which returns JSON-formatted data after serializing the DOM element instead of returning a string. The jquery code is as follows:
var fields = $ (": Checkbox,:radio"). Serializearray ();
Console.log (fields); Output with Firebug
The $.param () method is the core of the Serialize () method, which is used to serialize an array or an object according to Key/value.
For example, to serialize an ordinary object:
var obj = {A:1,b:2,c:3};
var k = $.param (obj);
Alert (k); Output a=1&b=2&c=3
2.serializeArray () method
Format: var jsondata = $ ("form"). Serializearray ();
Function: Serializes a page form into a JSON-structured object. Note that it is not a JSON string.
For example, [{"Name": "Lihui", "Age": "20"},{...}] get data for Jsondata[0].name