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