This article will introduce some usage of serialize (), serializeArray (), And param () methods in jQuery. I hope this article will help you.
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 |
$ ("# Form1"). serialize (); |
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.
There is also a method similar to serialize () in JQuery -- serializeArray (). This method does not return a string, but returns JSON-format data after the DOM element is serialized. 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:
The Code is as follows: |
Copy code |
Var obj = {a: 1, B: 2, c: 3 }; Var k = $. param (obj ); Alert (k); // output a = 1 & B = 2 & c = 3 |
View instances
The following is the jsp code of the server:
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> "); %> |
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 |
$ ("# Send"). click (function (){ $. Get ("get1.jsp", $ ("# form1"). serialize (), function (data, textStatus) $ ("# ResText" cmd.html (data ); }); }); |
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.