Analysis of serialize () usages in jquery _jquery

Source: Internet
Author: User
Tags serialization

This example describes the serialize () usage in jquery. Share to everyone for your reference. The specific analysis is as follows:

Serialize () Definition and usage:

The serialize () method creates a standard URL-encoded text string that represents a jquery object representing a collection of form elements by serializing the form value. You can select one or more form elements (such as input or text boxes), or the form element itself. The serialized value can be used in the URL query string when generating an AJAX request.

Grammar:

$ (selector). Serialize ()

Detailed description

1. The. Serialize () method creates a text string represented by a standard URL encoding. Its action object is a JQuery object that represents a collection of form elements.

2. The. Serialize () method can manipulate JQuery objects that have selected individual form elements, such as <input>, <textarea>, and <select>. However, it is generally easier to select <form> label itself for serialization

3, only "successful controls" will be serialized as strings. If you do not use a button to submit the form, the value of the Submit button is not serialized. If you want the value of the form element to be included in the sequence string, the element must use the Name property.

4, the form inside the name can not be enough Js, jquery in the keyword.
For example: length

Copy Code code as follows:
<form id= "Form1" >
<input name= "Length" type= "text" value= "Pipi"/>
<input name= "blog" type= "text" value= "Blue Submarine"/>
</form>

Use: $ ("#form1"). Serialize ();

The value is not obtained on the above.

Examples of serialize () in jquery

1, Ajax serialize ()

Copy Code code as follows:
$.ajax ({
Type: "POST",
DataType: "JSON",
Url:ajaxcallback,
data:$ (' #myForm '). Serialize (),//ID to submit the form
Success:function (msg) {
Alert (msg);
}
});

2, serialize () serialization of form instances

Copy Code code as follows:
<script src= "Jquery-1.7.min. JS "></script>
<script>
$ (function () {
$ ("#submit"). Click (function () {
Alert ($ ("#myForm"). Serialize ());
});
});
</script>
<form id= "MyForm" >
Nickname <input type= "text" name= "username" value= "admin"/><br/>
Password <input type= "password" name= "password" value= "admin123"/><br/>
<input type= "button" id= "Submit" value= "serialized form"/>
</form>

Click the button and then eject:

Username=admin&password=admin123

Third, serialize is a simple package of Serializearray with Param method

1, $.param ()

The $.param () method is the core of the Serialize () method, which is used to serialize an array or object by Key/value.

param method of JS code

Copy Code code as follows:
Param:function (a) {
<summary>
This is internal. Use serialize () instead.
</summary>
<param name= "A" type= "map" >a map of Key/value pairs to serialize into a string.</param> '
<returns type= "String"/>
<private/>

var s = [];

function Add (key, value) {
s[S.length] = encodeURIComponent (key) + ' = ' + encodeuricomponent (value);
};

If an array being passed in, assume that it's an array
of form elements
if (Jquery.isarray (a) | | a.jquery)
Serialize the form elements
Jquery.each (A, function () {
Add (this.name, this.value);
});

Otherwise, assume, it ' s an object of key/value pairs
Else
Serialize the Key/values
for (Var j in a)
If The value is a array then the key names need to be repeated
if (Jquery.isarray (A[j]))
Jquery.each (A[j], function () {
Add (J, this);
});
Else
Add (J, Jquery.isfunction (A[j])? A[j] (): A[j]);

Return the resulting serialization
Return S.join ("&"). Replace (/%20/g, "+");
}

For example:

Copy Code code as follows:
var obj = {A:1,b:2,c:3};
var k = $.param (obj);
Alert (k); Output a=1&b=2&c=3


2, Serializearray

The Serializearray method is to serialize each field in a form into an array
jquery Definition of Serializearray method
Copy Code code as follows:
Serializearray:function () {
<summary>
Serializes all forms and form elements but returns a JSON data structure.
</summary>
<returns type= "String" >a JSON data structure representing the serialized items.</returns>

Return This.map (function () {
Return this.elements? Jquery.makearray (this.elements): this;
})
. filter (function () {
return this.name &&!this.disabled &&
(this.checked | |/select|textarea/i.test (this.nodename) | |
/text|hidden|password|search/i.test (This.type));
})
. map (function (i, elem) {
var val = jQuery (This). Val ();
return val = null? Null:
Jquery.isarray (val)?
Jquery.map (Val, function (val, i) {
return {name:elem.name, value:val};
}) :
{name:elem.name, value:val};
}). get ();
}

Serializearray Data Example:

Copy Code code as follows:
[ {
Name:username,
Value: China
}, {
Name:password,
Value:xxx
}]

I hope this article will help you with your jquery programming.

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.