Ajax--serialize ()/serializearray ()/param () method in jquery source code

Source: Internet
Author: User
Tags javascript array

Due to jqueryobject. the core of the serialize () method is the $.param () method, so learn the $.param () method first.

One, $.param () method

The $.param () method is used to serialize an array or object according to Key/value for use in URL query strings or AJAX requests . The string it returns has been URL-encoded (with a character set of UTF-8).

Grammar:

Jquery.param (obj [, traditional])

The parameters are as follows:

Parameters Description
Obj The JS object that needs to be serialized.
Traditional Indicates whether to perform a traditional "shallow" serialization.

For parameter traditional You can view: http://www.365mini.com/page/jquery_param.htm

Example:

var obj = {        A:1,        B:2,        C:3        }; var k = $.param (obj); alert (k);

return Result:

Source code for the $.param () Method: (Path:jquery/src/serialize.js)

//Serialize An array of form elements or a set of//key/values into a query stringJquery.param =function(A, traditional) {varprefix, s=[], add=function(key, valueorfunction) {//If value is a function, invoke it and use its return value            varValue = Jquery.isfunction (valueorfunction)?valueorfunction (): valueorfunction; s[S.length]= encodeURIComponent (key) + "=" +encodeURIComponent (Value==NULL? "": Value);    }; //If An array were passed in, assume that it's an array of form elements.    if(Jquery.isarray (a) | | (A.jquery &&!)Jquery.isplainobject (a))) {        //Serialize the form elementsJquery.each (A,function() {Add ( This. Name, This. Value);    } ); } Else {        //If Traditional, encode the "old" (the 1.3.2 or older        //Did it), otherwise encode params recursively.         for(Prefixincha) {buildparams (prefix, a[prefix], traditional, add); }    }    //Return The resulting serialization    returnS.join ("&" );};

In this part of the code:

    • The decision is to serialize using traditional or non-traditional methods, and if traditional, call the Buildparams method.
    • Serialize each item and store it in an array of strings
    • String data is finally concatenated with the symbol "&"

Second, Jqueryobject.serializearray () method

Jqueryobject. The Serializearray () method is used to serialize a set of form elements and encode the contents of the form into a single JavaScript array. Often used to serialize the contents of a form into a JSON object for easy encoding into a JSON-formatted string.

Grammar:

Jqueryobject.  Serializearray()   

Example:

<! DOCTYPE html>        $(function(){            $("#send"). On ("click",function() {                varFields = $ ("Form1"). Serializearray ();            Console.log (fields);        });            }); </script>

return Result:

Source code for the Jqueryobject.serializearray () Method: (Link:jquery/src/serialize.js)

This piece of code to paste the great God Comment: (link: http://blog.csdn.net/liangklfang/article/details/49661023)

Serializearray:function() {        //if there is elements then get the elements of the element, otherwise it is itself!        return  This. Map (function() {            //Can add Prophook for ' elements ' to filter or add form elements            varelements = Jquery.prop ( This, "Elements" ); returnElements? Jquery.makearray (elements): This; }). Filter (function() {            varType = This. Type; //The filter method is already a DOM element.            //Use . are (":d isabled") so that fieldset[disabled] works            //must have the Name property, and the element is not a hidden element, while nodename must be a Input/select/textarea/kengen element            //at the same time the type of the element must not be of type sumbit/button/image/reset/file, and the element's checked attribute must exist            //If checkded does not exist, then the element type must not be a Checkbox/radio type            return  This. Name &&!jquery ( This). Is (":d isabled") &&Rsubmittable.test ( This. NodeName) &&!rsubmittertypes.test (type) &&                (  This. checked | | !rcheckabletype.test (type)); }). Map (function(i, elem) {//This really handles the elements .            //Gets the value of the element, if the value is NULL then the value returned is null            //if the value of the element exists, and if the value is an array, the            //The value of the element is iterated, put into an object, name is the element's names, the key value is the value of the element, but the value is to take the value of carriage return to newline!             varval = JQuery ( This). Val (); returnval = =NULL?NULL: Jquery.isarray (val)?Jquery.map (Val,function(val) {return{name:elem.name, Value:val.replace (Rcrlf, "\ r \ n" ) }; }): {name:elem.name, Value:val.replace (Rcrlf,"\ r \ n" ) };    }). get (); }

Third, Jqueryobject.serialize () method

    • serialize()function is used to serialize a set of form elements and encode the contents of the form into a string for submission.
    • serialize()Functions are often used to serialize the contents of a form for use in AJAX submissions.
    • The function is mainly based on the name and value of the valid form control used for submission, stitching them into a text string that can be directly used for form submission, which has been processed by standard URL encoding (character set encoding UTF-8).

Grammar:

Jqueryobject.serialize ()

Example:

<! DOCTYPE html>        $(function(){            $("#send"). On ("click",function() {                //var fields = $ ("#form1"). Serializearray ();                varresult = $ ("#form1"). Serialize ();            Console.log (result);        });    }); </script>

The results are as follows:

The source code for the Jqueryobject.serialize () method is as follows:

function () {        returnthis. Serializearray ());    }

  * So the whole process of jqueryobject.serialize () is: Call Jqueryobjec first T.serializearray () method encode the jqueryobject into a JavaScript array, and then call the Jquery.param () method to serialize the data, returning a URL-encoded string.

*jquery.param () is a global function, Jqueryobject.serializearray (), jqueryobject.serialize () belong to the JQuery object.

Ajax--serialize ()/serializearray ()/param () method in jquery source code

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.