In JavaScript, the Serialize () method also acts on a jquery object, which serializes the contents of the DOM element into a string, the Serializearray () method is not the return string, but instead serializes the DOM element. Returns data in JSON format.
Server-side JSP code:
Import= "java.util.*" pageencoding= "UTF-8"%> <% request.setcharacterencoding ("UTF-8" = Request.getparameter ("username"= Request.getparameter ("content"); Out.println ("<div class= ' comment ' >
Like other methods in jquery, the Serialize () method acts on a jquery object, which serializes the contents of the DOM elements into strings for Ajax requests. By using the Serialize () method, you can submit all fields of this page with the following code:
$ ("#send"). Click (function () {
$.get ("get1.jsp", $ ("#form1"). Serialize (), function (data, textstatus)
$ ("#resText"). HTML (data);
}); Www.jbxue.com
});
When you click the Submit button, all form elements that belong to Form1 can be submitted to the background, and even if you add fields to the form, the script is still available and does not require any extra work.
In string mode, you need to be aware of the character encoding (Chinese problem), if you do not want coding to cause trouble, you can use the serialize () method, it will be automatically encoded. (www.jbxue.com)
Because the serialize () method is used for jquery objects, it can be used not only by the form, but also by the elements selected by other selectors, such as the following jquery code:
$ (": Checkbox,:radio"). Serialize ();
Serializes the value of a check box and a radio box to a string, only the selected value is serialized.
The
also has a method similar to serialize () in jquery,--serializearray (), which returns JSON-formatted data after serializing the DOM element instead of returning a string. The jquery code is as follows:
var fields = $ (": Checkbox,:radio"). Serializearray ();
Console.log (fields); The Firebug output
$.param () method is the core of the Serialize () method, which is used to serialize an array or object according to Key/value.
For example, serialization of a normal object:
var obj = {A:1,b:2,c:3};
var k = $.param (obj);
Alert (k);//Output a=1&b=2&c=3