Examples of serialize (), Serializearray (), and Param () methods in jquery are described _jquery

Source: Internet
Author: User

The following is the server-side JSP 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 ' > 
 

Like other methods in jquery, the Serialize () method acts on a jquery object, which serializes the DOM element content into a string for AJAX requests. By using the Serialize () method, you can submit all the fields in this page with the following code:

$ ("#send"). Click (function () { 
$.get ("get1.jsp", $ ("#form1"). Serialize (), function (data, textstatus) 
$ ("# ResText "). HTML (data); 
});

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 can still be used and no extra work is required.

When you use a string, you need to be aware of the character encoding (Chinese problem), and if you don't want the encoding to be troublesome, you can use the serialize () method, which is automatically encoded.

Because the serialize () method is used for jquery objects, it is not the only form that can be used, but also 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, and only serializes the selected value.

In jquery There is also a method similar to serialize ()--serializearray (), which returns the data in JSON format after serializing the DOM elements instead of returning a string. The jquery code is as follows:

var fields = $ (": Checkbox,:radio"). Serializearray ();
Console.log (fields); Output with Firebug

The $.param () method is the core of the Serialize () method, which is used to serialize an array or object by Key/value.

such as 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.