Examples of serialize (), Serializearray (), and Param () usage in jquery

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.