JQuery serialized form Data serialize () Serializearray ()

Source: Internet
Author: User

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

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.