jQuery ajax - serialize() method
Example
Output the result of serializing the form values:
$("button").click(function(){ $("div").text($("form").serialize()); });
Definition and Usage
The serialize() method creates a URL-encoded text string by serializing form values.
You can select one or more form elements (such as input and/or text box), or the form element itself.
The serialized values can be used in the URL query string when generating AJAX requests.
Syntax
$(selector).serialize()
Detailed Description
.serialize() method creates a text string represented in standard URL encoding. Its operation object is a jQuery object representing a collection of form elements.
There are several types of form elements:
<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="40">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>
.serialize() method can operate on jQuery objects selected from individual form elements, such as <input>, <textarea>, and <select>. However, it is generally easier to serialize the <form> tag itself:
$('form').submit(function() { alert($(this).serialize()); return false; });
Output the standard query string:
a=1&b=2&c=3&d=4&e=5
Note:Only serializes the 'successful control'. If the form is not submitted using a button, the value of the submit button is not serialized. If the value of the form element needs to be included in the serialized string, the element must use the name attribute.