Instance
Output serialized form value results:
Copy codeThe Code is as follows:
$ ("Button"). click (function (){
$ ("Div"). text ($ ("form"). serialize ());
});
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 form elements.
Serialized values can be used in URL query strings when an AJAX request is generated.
Syntax
Copy codeThe Code is as follows:
$ (Selector). serialize ()
Detailed description
The. serialize () method creates a text string encoded with a standard URL. Its operation object is a jQuery object that represents the form Element Set.
There are several types of form elements:
Copy codeThe Code is as follows:
<Form>
<Div> <inputtype = "text" name = "a" value = "1" id = "a"/> </div>
<Div> <inputtype = "text" name = "B" value = "2" id = "B"/> </div>
<Div> <inputtype = "hidden" name = "c" value = "3" id = "c"/> </div>
<Div>
<Textareaname = "d" rows = "8" cols = "40"> 4 </textarea>
</Div>
<Div> <selectname = "e">
<Optionvalue = "5" selected = "selected"> 5 </option>
<Optionvalue = "6"> 6 </option>
<Optionvalue = "7"> 7 </option>
</Select> </div>
<Div>
<Inputtype = "checkbox" name = "f" value = "8" id = "f"/>
</Div>
<Div>
<Inputtype = "submit" name = "g" value = "Submit" id = "g"/>
</Div>
</Form>
The. serialize () method can operate jQuery objects that have selected individual form elements, such as <input>, <textarea>, and <select>. However, it is easier to select the <form> label itself for serialization:
$ ('Form'). submit (function (){
Alert ($ (this). serialize ());
Returnfalse;
});
Output standard query string:
A = 1 & B = 2 & c = 3 & d = 4 & e = 5
Note: Only "successful controls" are serialized as strings. If the button is not used to submit a form, the value of the submit button is not serialized. To include the value of a form element in a sequence string, the element must use the name attribute.