This article mainly introduces serialize () serialization in JQuery, which is very detailed and comprehensive. We recommend this article to you: In jQuery, when we use ajax, it is often necessary to assemble the input data to be sent to the server in the form of Key-Value pairs (Key/Value). The JQuery serialize method can be used to easily complete this task, this method can be used to serialize a form into a key-Value Pair (key1 = value1 & key2 = value2 ...) And then submit. The following describes how to use serialize () in JQuery.
I. serialize () Definition and usage:
The serialize () method creates a standard URL-encoded text string by serializing the form value. Its operation object is a jQuery object representing the form Element Set. You can select one or more form elements (such as input or text box) or form elements. Serialized values can be used in URL query strings when an AJAX request is generated.
Syntax:
The Code is as follows:
$ (Selector). serialize ()
Detailed description
1. The serialize () method creates a text string encoded with a standard URL. Its operation object is a jQuery object that represents the form Element Set.
2. The serialize () method can operate jQuery objects that have selected individual form elements, for example,
3. Only "successful controls" are serialized as strings. If the button is not used to submit a form, the value of the submit button is not serialized. To include the value of a form element in a sequence string, the element must use the name attribute.
4. The name in form cannot use keywords in Js or jquery.
Example: length
The Code is as follows:
// Use: $ ("# form1"). serialize ();
The value cannot be obtained.
Ii. serialize () instance in JQuery
1. ajax serialize ()
The Code is as follows:
$. Ajax ({
Type: "POST ",
DataType: "json ",
Url: ajaxCallBack,
Data: $ ('# myForm'). serialize (), // ID of the form to be submitted
Success: function (msg ){
Alert (msg );
}
});
2. serialize () serialized form instance
The Code is as follows: