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:
The code is as follows: |
Copy code |
<Form id = "form1"> <Input name = "name" type = "text" value = "pipi"/> <Input name = "blog" type = "text" value = "blue submarine"/> </Form> Usage: $ ("# Form1"). serialize (); Result: Name1 = pipi & blog = blue + submarine |
That is, how can I change the plus sign back to a space?
Another problem is as follows:
The code is as follows: |
Copy code |
<Form id = "form1"> <Input name = "length" type = "text" value = "pipi"/> <Input name = "blog" type = "text" value = "blue submarine"/> </Form> |
Usage:
$ ("# Form1"). serialize ();
Result:
Blog = blue + submarine
Length = pipi cannot appear
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:
The code is as follows: |
Copy code |
$ (": Checkbox,: radio"). serialize (); |
Serialize the check box and single-byte values into strings. Only the selected values are serialized.
In JQuery, there is another method similar to serialize ()-serializeArray (). This method does not return a string, but serializes the DOM element and returns JSON data. The JQuery code is as follows:
The code is as follows: |
Copy code |
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 |
Application in ajax
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:
The code is as follows: |
Copy code |
<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> 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 ()); Return false; }); Output standard query string: A = 1 & B = 2 & c = 3 & d = 4 & e = 5 |
You can also do this.
The code is as follows: |
Copy code |
$ ("# Send"). click (function (){ $. Get ("get1.jsp", $ ("# form1"). serialize (), function (data, textStatus) $ ("# ResText" cmd.html (data ); }); }); |
In this way, the data in the past can be accepted according to the ajax post type.
The code is as follows: |
Copy code |
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> <% Request. setCharacterEncoding ("UTF-8 "); String username = request. getParameter ("username "); String content = request. getParameter ("content "); Out. println ("<div class = 'comment'> </P> </div> "); %> |