Play Web JSON (v)---Convert the value obtained by the form through the Serialize () method to JSON

Source: Internet
Author: User

The form form has a serialize () method. The ability to serialize the values of the form, but this method of jquery provides a way to serialize the data into a form similar to the following:

A=1&b=2&c=3&d=4

jquery does not provide a way to serialize form data to JSON. So we need to encapsulate it ourselves. The following gives the code and test examples, first write a JS file:

$.ajaxsetup ({    contentType: "application/x-www-form-urlencoded; Charset=utf-8 "}); var datadeal = {//will be passed from form to $ (' #form '). Serialize () Gets the value to be converted to JSON   formtojson:function (data) {   Data=data.replace (/&/g, "\", \ "");               Data=data.replace (/=/g, "\": \ "");               Data= "{\" "+data+" \ "}";               return data;    },};

To use: First in the page to import the above JS file, and then

        var data=$ (' #addf '). Serialize ();//Get Value        data= decodeuricomponent (data,true);//Prevent Chinese garbled        var json= Datadeal.formtojson (data);//Convert to JSON


Play Web JSON (v)---Convert the value obtained by the form through the Serialize () method 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.