In the process of daily development, inevitably use the form form, we need to get the form of data to save to the database, or get the back of a bunch of JSON data, to bind the data to the form form, here I wrote a jquery based, Formhelp Plug-ins, use it is very simple:
Gets the data for the form:$ ("#formid"). Serializejson ();
Bind data to form:$ ("#formid"). Setform (JSON);
Jquery.formHelp.js Plugin
/** * Serializes the contents of a form into JSON * The same checkbox is spliced together with a semicolon * @param {dom} specifies a selector * @param {obj} needs to be spliced in the back of the JSON object * @method Serializ
Ejson * * */$.fn.serializejson=function (otherstring) {var serializeobj={}, Array=this.serializearray (); $ (array). each (function () {if (Serializeobj[this.name]) {serializeobj[this.name]+= '; '
+this.value;
}else{Serializeobj[this.name]=this.value;
}
});
if (otherstring!=undefined) {var otherarray = Otherstring.split (';');
$ (Otherarray). each (function () {var othersplitarray = this.split (': ');
SERIALIZEOBJ[OTHERSPLITARRAY[0]]=OTHERSPLITARRAY[1];
});
return serializeobj;
}; /** * Josn Object assigned to form * @param {dom} selector * @param {obj} needs to assign a value to the form's JSON object * @method Serializejson * * * * * * * $.fn.setform
= function (jsonvalue) {var obj = this;
$.each (Jsonvalue,function (name,ival) {var $oinput = Obj.find ("input[name=" +name+ "]"); if ($oinput. attr ("type") = = "checkbox") {if (ival!== null) {var CHECKBOXobj = $ ("[name=" +name+ "]");
var Checkarray = Ival.split (";"); for (Var i=0;i<checkboxobj.length;i++) {for (Var j=0;j<checkarray.length;j++) {if (checkboxobj[i)
. Value = = Checkarray[j]) {Checkboxobj[i].click (); else if ($oinput. attr ("type") = = "Radio") {$oinput. each (function () {V
AR radioobj = $ ("[name=" +name+ "]");
for (Var i=0;i<radioobj.length;i++) {if (Radioobj[i].value = = ival) {Radioobj[i].click ();
}
}
});
else if ($oinput. attr ("type") = = "textarea") {Obj.find ("[Name=" +name+ "]"). HTML (ival);
} else{Obj.find ("[Name=" +name+ "]"). Val (ival);
}
})
}
HTML test Code
The above is jquery to get form form content and bind data to form form all content, hope to be helpful to everybody's study.