How to use Serialize (), Serializearray (), and Param () in jquery

Source: Internet
Author: User

1. Serialize () method
Syntax:$ (selector). Serialize ()
The output serialized form string, the file upload control (for example: <input type= "file" name= "MyFile"/>) type of form data is automatically ignored, that is, does not appear in the serialization string.

For example, there is a form such as the following, with $ ("form"). Serialize () The result of serialization is: a=1&b=2&c=3

<form>  <div><input type= "text" Name= "a" value= "1" id= "a"/></div>  <div>< Input type= "text" name= "B" value= "2" id= "B"/></div>  <div><input type= "hidden" name= "C" value= "3 "Id=" C "/></div>    <div><input type=" file "Name=" MyFile "/></div></form>

The serialize () method can also manipulate JQuery objects that have selected individual form elements, such as <input>, <textarea> and <select>, and only need to modify the selector.

2. Param () method
This method is simple, which is to serialize a Key/value object, see the following example:

var params = {width:1900, height:1200};var str = jquery.param (params); $ ("#results"). Text (str);

Results:
width=1680&height=1050

3. Serializearray () method
The Serializearray () method creates an array of objects (names and values) by serializing the form values.
You can select one or more form elements (such as input and/or textarea), or the form element itself.
The Serializearray () method serializes the form element (similar to the. Serialize () method) and returns the JSON data structure.
Note: This method returns a JSON object rather than a JSON string. You need to use a plug-in or a third-party library for string manipulation.
The returned JSON object consists of an array of objects, each of which contains one or two name-value pairs of the--name parameter and the value parameter if value is not empty. For example:

[   {name: ' FirstName ', Value: ' Hello '},   {name: ' LastName ', Value: ' World '},  {name: ' Alias '},//value is empty]

The. Serializearray () method uses the standard of the successful controls (active control) to detect which elements should be included. Specifically, the element cannot be disabled (disabled elements are not included), and the element should have a name attribute. The value of the Submit button is also not serialized. The data for the file selection element is also not serialized.

How to use Serialize (), Serializearray (), and Param () in 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.