jquery Ajax Serialize () instance

Source: Internet
Author: User

. Serialize ()
Returns:string
Description:encode a set of form elements as a string for submission.

Version added:1.0.serialize ()

A collection object that runs in a jquery representing a form of element. The table element can be of the following types

<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 process a jquery object, select individual form elements, such as <input>, <textarea>, and < selections. However, it is usually easier to choose <form> mark as serialization itself:

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


This produces a standard-looking query string:

A=1&b=2&c=3&d=4&e=5

Only "successful control" is serialized to a string. The value of no submit button is serialized as the use button is not submitted. There must be a name attribute for the element contained in the serialized string for the value of the form element. The data in the selection element from the file is not serialized.

For example:
The serialized form of the query string, which may be an AJAX request sent to the server.


<!doctype html>
<style>
Body, select {font-size:12px;}
form {margin:5px;}
p {color:red; margin:5px; font-size:14px;}
b {color:blue;}
</style>
<script src= "Http://code.jquery.com/jquery-latest.min.js" > <body>

<form>
<select name= "single" >
<option>single</option>
<option>single2</option>
</select>

<br/>
<select name= "multiple" multiple= "multiple" >
<option selected= "Selected" >multiple</option>
<option>multiple2</option>

<option selected= "Selected" >multiple3</option>
</select>
<br/>
<input type= "checkbox" name= "Check" value= "Check1" id= "Ch1"/>

<label for= "Ch1" >check1</label>

<input type= "checkbox" name= "Check" value= "Check2" checked= "Checked" id= "CH2"/>

<label for= "CH2" >check2</label>
<br/>
<input type= "Radio" name= "Radio" value= "Radio1" checked= "checked" id= "R1"/>

<label for= "R1" >radio1</label>
<input type= "Radio" name= "Radio" value= "Radio2" id= "R2"/>

<label for= "R2" >radio2</label>
</form>
<p><tt id= "Results" ></tt></p>
<script>
function ShowValues () {
var str = $ ("form"). Serialize ();
$ ("#results"). Text (str);
}
$ (": CheckBox,: Radio"). Click (showvalues);
$ ("select"). Change (ShowValues);
ShowValues ();
</script>

</body>

 

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.