Home  >  Article  >  Web Front-end  >  serialize() serialization_javascript skills in JQuery

serialize() serialization_javascript skills in JQuery

PHP中文网
PHP中文网Original
2016-05-16 16:09:471333browse

Introduction to this article: In jQuery, when we use ajax, we often need to assemble input data and send it to the server in the form of key/value pairs. This work can be easily completed using JQuery's serialize method. Use this The method can serialize the form into key-value pairs (key1=value1&key2=value2…) and submit it. The following introduces the usage of serialize() in JQuery

1. Definition and usage of serialize():

The serialize() method creates a A standard URL-encoded text string that operates on a jQuery object representing a collection of form elements. You can select one or more form elements (such as inputs or text fields), or the form element itself. Serialized values ​​can be used in URL query strings when making AJAX requests.

Syntax:


Copy code The code is as follows:


$(selector).serialize( )

Detailed description

1. The .serialize() method creates a text string represented by standard URL encoding. It operates on a jQuery object that represents a collection of form elements.

2. The .serialize() method can operate jQuery objects that have selected individual form elements, for example,

3. Only "successful controls" will be serialized into strings. If you do not use a button to submit the form, the submit button's value is not serialized. If you want a form element's value to be included in a sequence string, the element must use a name attribute.

4. The name in the form cannot use keywords in Js or jquery.

For example: length


Copy code The code is as follows:



//Use :$("#form1").serialize();

The above value cannot be obtained.

2. Examples of serialize() in JQuery

1. ajax serialize()


Copy the code The code is as follows:


$.ajax({
type: "POST",
dataType: "json",
url:ajaxCallBack,
data:$('#myForm').serialize(),// ID of the form to be submitted
success: function(msg){
alert(msg);
}
});

2. serialize() serialize form instance


Copy code The code is as follows:


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn