JQuery serialize () serializeArray (), jquery form serialization

Source: Internet
Author: User

JQuery serialize () serializeArray (), jquery form serialization

1. serialize () method

Format: var data = $ ("form"). serialize ();

Function: serialize the form content into a string.

In this way, when submitting form data through ajax, you do not need to list every parameter one by one. Set the data parameter to $ ("form"). serialize.

 

Like other methods in JQuery, The serialize () method also acts on a JQuery object, which can serialize the content of DOM elements into strings for ajax requests. By using the serialize () method, you can submit all the fields on this page. The Code is as follows:

 

1 $("#send").click(function(){2 3      $.get("get1.jsp", $("#form1").serialize(), function(data, textStatus)4 5               $("#resText").html(data);6 7      });8 9 });

 

After you click the "Submit" button, all form elements of form1 can be submitted to the background. Even if fields are added to the form, the script can still be used without additional work.

 

When using the string method, you need to pay attention to the character encoding (Chinese problem). If you do not want the encoding to be troublesome, you can use the serialize () method, which will be automatically encoded.

 

Because the serialize () method acts on JQuery objects, it can be used not only for forms, but also for elements selected by other selectors, as shown in the following JQuery code:

$ (": Checkbox,: radio"). serialize ();

Serialize the check box and single-byte values into strings. Only the selected values are serialized.

 

There is also a method similar to serialize () in JQuery -- serializeArray (). This method does not return a string, but returns JSON-format data after the DOM element is serialized. The JQuery code is as follows:

Var fields = $ (": checkbox,: radio"). serializeArray ();

Console. log (fields); // output with FireBug

 

$. Param () is the core of the serialize () method. It is used to serialize an array or object according to key/value.

For example, serialize a common 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: serialize a page form into a JSON object. Note that it is not a JSON string.

For example, if [{"name": "lihui", "age": "20" },{...}] returns jsonData [0]. name

 


Jquery can be used to serialize form values using serialize (). Is there any way to fill the values in the form?

Serialize () is used to generate a string similar to this format for ajax to submit a = 1 & B = 2 & c = 3. To enter a value in a form, you must first have a value, which is the id of the Form Control or the property that can uniquely locate the control. Then $ ("# id"). val ("value ")

Jquery ajax submits forms and serialize () serializes form data. However, it does not support type = file input, that is, uploading. How can this problem be solved?

You can upload images in advance. This problem does not occur if you upload images first and then submit images!
If you have to submit the file together, you can add the file to the submission sequence separately.
$ ("Form"). serialize () + "& fileInput =" + $ ("input [name = 'fileinputname']"). val (); this is also possible


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.