Example of serialize (), serializeArray (), And param () methods in JQuery: serializearray

Source: Internet
Author: User

Example of serialize (), serializeArray (), And param () methods in JQuery: serializearray

The following is the jsp code of the server:

<%@ 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 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:

$("#send").click(function(){ $.get("get1.jsp", $("#form1").serialize(), function(data, textStatus) $("#resText").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.

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:

$(":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:

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

How to Use jquery serialize to get only the value of the value part

Use serializeArray () to return JSON data and operate JSON data directly. The structure is as follows:
[{Name: 'firstname', value: 'hello'}, {name: 'lastname', value: 'World'}, {name: 'Alias '}, // The value is null.]

X = $ ("form"). serializeArray ();
Alert (x [0]. value); // The value of 'firstname' is 'Hello'
Alert (x [1]. value); // 'lastname' value 'World'

$ ("# ActionForm") formSerialize () Error in JQuery: the object does not support the "formSerialize" attribute or method. Why?

As far as I know, formSerialize () is the method provided in jQuery's form plug-in.
The core method of formSerialize () is: $. param (data );
Form is serialized as a string, for example, name1 = value1 & name2 = value2, after formSerialize ().
You must use a string variable to pick up the serialized content.
① Var queryString = $ ('# myFormId'). formSerialize ();
② Var queryString = $. param ($ ('# myFormId '));
① And ② are equivalent, except formSerialize (); provided by the jQuery. form plug-in, while $. param (data); is provided by jQuery native.
Your question:
"The object does not support" formSerialize "attributes or methods"
Cause:
① The file jQuery. Form. js was not referenced before formSerialize was called.
② $ ("# ActionForm"). formSerialize (); In the js file, called before referencing jQuery. Form. js, this method will not be found.

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.