A brief introduction to the Serialize () serialization of jquery

Source: Internet
Author: User

The serialize () serialization of jquery is a simple introduction:

In jquery, when using Ajax, it is often necessary to assemble input data to be sent to the server in the form of a key-value pair (key/value).

The Serialize method can do this easily, using this method to serialize the form to a key-value pair (key1=value1&key2=value2) and submit it.

The following describes the use of serialize () in jquery:

I. Serialize () Definition and usage:

The serialize () method creates a standard URL-encoded text string by serializing the form value, whose operand is a jquery object that represents a collection of form elements.

You can select one or more form elements (such as input or a text box), or the form element itself.

The serialized value can be used in the URL query string when generating an AJAX request. Syntax structure:

$ (selector). Serialize ()

Detailed Description:

(1) the. Serialize () method creates a text string that is represented as a standard URL encoding. Its operand 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 the <form> tag itself for serialization.

(3). Only "Successful controls" are serialized as strings. If you do not use a button to submit the form, the value of the Submit button is not serialized. If the value of the form element is to be included in the sequence string, the element must use the Name property.

(4). The name in the form cannot suffice in Js, jquery keyword.

For example Length:

<formID= "Form1">   <inputname= "Length"type= "text"value= "Pipi" />   <inputname= "blog"type= "text"value= "Blue submarine" /></form>//Use: $ ("#form1"). Serialize ();

The above does not get the value.

Two. Examples of serialize () in jquery:

(1). Ajax Serialize ():

$.ajax ({  "POST",  "JSON",  Url:ajaxcallback,  data:$ (' #myForm '). Serialize (),//  to submit the form's ID  function(msg) {    alert (msg);  }});

(2). Serialize () Serialization of form instances:

<Scriptsrc= "Jquery-1.7.min." JS "></Script><Script>$(function(){   $("#submit"). Click (function() {alert ($ ("#myForm"). Serialize ()); });});</Script><formID= "MyForm">Nickname<inputtype= "text"name= "username"value= "Admin" /><BR/>Password<inputtype= "Password"name= "Password"value= "admin123" /><BR/><inputtype= "button"ID= "Submit"value= "serialized form" /> </form>

Click the button and then pop up:

Username=admin&password=admin123

Three. Serialize is a simple wrapper for serializearray using the Param method:

1.$.param (): The $.param () method is the core of the Serialize () method, which is used to serialize an array or an object according to Keyalue

JS code for the Param method:

Paramfunction(a) {///<summary>        ///This method 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/>        vars = [ ]; functionAdd (key, value) {s[s.length]= encodeURIComponent (key) + ' = ' +encodeURIComponent (value);        }; //If An array were passed in, assume that it 's an array        //of form elements        if(Jquery.isarray (a) | |a.jquery)//Serialize the form elementsJquery.each (A,function() {Add ( This. Name, This. Value);        }); //Otherwise, assume that it's an object of Key/value pairs        Else            //Serialize the Key/values             for(varJincha)//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 );                }); ElseAdd (J, Jquery.isfunction (A[j])?A[j] (): A[j]); //Return The resulting serialization        returnS.join ("&"). Replace (/%20/g, "+")); }

For example:

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

The jquery definition of the Serializearray method:

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) {varval = JQuery ( This). Val (); returnval = =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 Examples:

[{   name:username,   value: China   }, {   Name:password,   value:xxx  }]

The original address is: http://www.51texiao.cn/jqueryjiaocheng/2015/0525/2461.html

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=17200

A brief introduction to the Serialize () serialization of jquery

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.