Use of the JQuery Ajax Serialize () method and common problem solving _jquery

Source: Internet
Author: User
When using AJAX, often need to assemble input data for ' name=abc&sex=1 ' this form, with jquery serialize method can easily complete this work!

JQuery ajax-serialize () method definition and usage
The serialize () method creates a URL-encoded text string by serializing the form value.
You can select one or more form elements (such as input and/or text boxes), or the form element itself.
The serialized value can be used in the URL query string when generating an AJAX request.

JQuery ajax-serialize () method syntax
$ (selector). Serialize ()
JQuery ajax-serialize () method detailed description

The. Serialize () method creates a text string that is represented by a standard URL encoding. Its action object is a JQuery object that represents a collection of form elements.

The jquery ajax-serialize () method has several types of form elements:
Copy Code code as follows:

<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>
<textarea name= "D" rows= "8" cols= ">4</textarea>"
</div>
<div><select name= "E" >
<option value= "5" selected= "selected" >5</option>
<option value= "6" >6</option>
<option value= "7" >7</option>
</select></div>
<div>
<input type= "checkbox" Name= "F" value= "8" id= "F"/>
</div>
<div>
<input type= "Submit" name= "G" value= "Submit" id= "G"/>
</div>
</form>

The. Serialize () method can manipulate JQuery objects that have selected individual form elements, such as <input&gt, <textarea>, and <select>. However, it is generally easier to choose <form> label itself to serialize:
Copy Code code as follows:

$ (' form '). Submit (function () {
Alert ($ (this). Serialize ());
return false;
});

Output standard query string:
A=1&b=2&c=3&d=4&e=5
JQuery ajax-serialize () method Note: 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 you want the value of the form element to be included in the sequence string, the element must use the Name property.

The above jquery ajax-serialize () method basic content turns to the common system, below explains several frequently asked questions which the jquery ajax-serialize () method appears below to share to everybody
Take a look at the following examples:
Copy Code code as follows:

<form id= "Form1" >
<input name= "name1" type= "text" value= "Pipi"/>
<input name= "name2" type= "Radio" value= "1" checked/>boy
<input name= "name2" type= "Radio" value= "0"/>girl
<textarea name= "Name3" >test</textarea>
</form>

Use: $ ("#form1"). Serialize ();
Result: Name1=pipi&name2=1&name3=test
There's a problem with the jquery ajax-serialize () method.
If this is the case below:
Copy Code code as follows:

<form id= "Form1" >
<input name= "name" type= "text" value= "Pipi"/>
<input name= "blog" type= "text" value= "Blue Submarine"/>
</form>

Use: $ ("#form1"). Serialize ();
Result: Name1=pipi&blog=blue+submarine
Is how can let the + number change go home lattice?
Finally, there is a problem, as follows:
Copy Code code as follows:

<form id= "Form1" >
<input name= "Length" type= "text" value= "Pipi"/>
<input name= "blog" type= "text" value= "Blue Submarine"/>
</form>

Use: $ ("#form1"). Serialize ();
Result: Blog=blue+submarine cannot appear length=pipi
The reason is that length is the property keyword of the JS array, there is a conflict, change name to another non-conflict string
Related Article

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.