Convert jquery serialized form values to JSON

Source: Internet
Author: User

From : 1190000000473625

The child has a form that he wants to get to the contents of the form in JSON. The children tried the following ways.

Through $ ("#form"). Serialize () can get a serialized form value string.

a=1&b=2&c=3&d=4&e=5

The form value is serialized as an array by $ ("#form"). Serializearray () output.

[   ‘firstname‘, value: ‘Hello‘},   {name: ‘lastname‘, value: ‘World‘}, {name: ‘alias‘}, // 值为空]

Nothing satisfies the child's desire to want JSON. After the stack overflow, a method was found

$.fn.serializeobject =function(){var o = {}; var a = this.serializearray (); $.each (A, function () {if (o [this.name]!== undefined) {if (! O[this.name].push) {o[this.name] = [o[ this.name]]; } o[this.name].push (this.value | | else {o[this.name] = this.value || return o;            

Then JSON.stringify($("#form").serializeObject()); you can get the JSON content by the chatter.

Convert jquery serialized form values to JSON

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.