JQuery ajax-serialize () method and FAQs

Source: Internet
Author: User

When using ajax, you often need to assemble the input data in the form of 'name = abc & sex = 1'. You can easily complete this work using the serialize method of JQuery! JQuery ajax-serialize () method definition and usage the serialize () method creates a URL-encoded text string by serializing the form value. You can select one or more form elements (such as input and/or text boxes) or form elements. Serialized values can be used in URL query strings when an AJAX request is generated. JQuery ajax-serialize () method syntax $ (selector). serialize () jQuery ajax-serialize () method detailed description. The serialize () method creates a text string represented by standard URL encoding. Its operation object is a jQuery object that represents the form Element Set. The jquery ajax-serialize () method has several types of form elements: <form> <div> <input type = "text" name = "a" value = "1" id = "a"/> </div> <input type = "text" name = "B" value = "2" id = "B"/> </div> <input type = "hidden" name = "c" value = "3" id = "c"/> </div> <textarea name = "d" rows = "8" cols = "40"> 4 </ textarea> </div> <select name = "e"> <option value = "5" selected = "selected"> 5 </option> <option value =" 6 "> 6 </option> <option valu E = "7"> 7 </option> </select> </div> <input type = "checkbox" name = "f" value = "8" id = "f"/> </div> <input type = "submit" name = "g" value = "Submit" id = "g"/> </div> </form>. the serialize () method can operate jQuery objects that have selected individual form elements, such as <input>, <textarea>, and <select>. However, it is easier to select the <form> label itself for serialization: $ ('form '). submit (function () {alert ($ (this ). serialize (); return false;}); output the standard query string: a = 1 & B = 2 & c = 3 & d = 4 & e = 5 jQuery ajax-serialize () method Note: only the "successful control" is serialized as a string. If the button is not used to submit a form, the value of the submit button is not serialized. To include the value of a form element in a sequence string, the element must use the name attribute. The basic content of the above jQuery ajax-serialize () method is transferred to W3C. The following describes several common problems that occur when using the jQuery ajax-serialize () method. The following is an example: <form id = "form1"> <input name = "name1" type = "text" value = "pipi"/> <input name = "name2" type = "radio" value = "1" checked/> boy <input name = "name2" type = "radio" value = "0"/> girl <textarea name = "name3"> test </textarea> </form> usage: $ ("# form1 "). serialize (); Result: name1 = pipi & name2 = 1 & name3 = test is returned using the jQuery ajax-serialize () method There is a problem in the following case: www.2cto.com <form id = "form1"> <input name = "name" type = "text" value = "pipi"/> <input name = "blog" type = "text" value = "blue submarine"/> </form> usage: $ ("# form1 "). serialize (); Result: name1 = pipi & blog = blue + submarine, How can I change the + sign back to a space? There is another problem as follows: <form id = "form1"> <input name = "length" type = "text" value = "pipi"/> <input name = "blog" type = "text" value = "blue submarine"/> </form> usage: $ ("# form1 "). serialize (); Result: blog = blue + submarine cannot show length = pipi. The reason is that length is the attribute keyword of the js array. If there is a conflict, change name to another non-conflicting string.

Related Article

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.