JS**JSON表单**
作者:互联网
/**** * @param idMap {insured:['insuredName','insuredAge'],applicant:['applicantName','applicantSex']} * @return * { * insured:{ * insuredName:"nick", * insuredAge:"30" * }, * applicant:{ * applicantName:"nick", * applicantSex:"1" * } * } */ cpic.formField2JSON = function(idMap) { var json = {}; var dom=null; for(var key in idMap){ json[key] = {}; for(var idx=0;idx<idMap[key].length;idx++) { if(!cpic.isNull(idMap[key][idx])) { dom = document.getElementById(idMap[key][idx]); if(dom) { if(!json[key][dom.name]) { json[key][dom.name]=""; } if(dom.type=="checkbox" ||dom.type=="radio") { if(dom.checked){ json[key][dom.name] = dom.value; } }else { json[key][dom.name] = dom.value; } } } } } return json; }; /** * 表单封装成JSON对象(有name属性的表单域) * @param formId 表单的id * return 指定表单的封装JSON格式对象(含name属性的表单) */ cpic.form2JSON = function(formId, context) { var form = null; if(context) { form = $("#" + formId,context)[0]; }else { form= document.getElementById(formId); } if(form) { var json={}; for(var _i=0;_i<form.length;_i++){ if(form[_i].name) { if(form[_i].type=="checkbox" && $(form[_i]).attr("single")=="true") {//single为true表示为是否勾选项 json[form[_i].name]="0"; if(form[_i].checked) { json[form[_i].name]="1"; } }else if(form[_i].type=="checkbox") { if(form[_i].checked) { if(cpic.isNull(json[form[_i].name])){ json[form[_i].name]=[]; } json[form[_i].name].push(form[_i].value); } }else if(form[_i].type=="radio"){ if(form[_i].checked) { json[form[_i].name]=form[_i].value; } }else { json[form[_i].name]=form[_i].value; } } } return json; }else { throw "FORM NOT EXISTS"; } }; /**** * JSON填充表单 */ cpic.JSON2form = function(formId, json, formContext){ var form = null; if(formContext) { form = $("#" + formId,formContext)[0]; }else { form= document.getElementById(formId); } if(form) { for(var _i=0;_i<form.length;_i++){ if(form[_i].name && json[form[_i].name]) { if(form[_i].type=="checkbox" && $(form[_i]).attr("single")=="true") { json[form[_i].name]=="1"?$(form[_i]).attr("checked","true"):$(form[_i]).attr("checked","false"); }else if(form[_i].type=="checkbox" || form[_i].type=="radio" ) { json[form[_i].name]==$(form[_i]).val()?$(form[_i]).attr("checked","true"):$(form[_i]).attr("checked","false"); }else { $(form[_i]).val(json[form[_i].name]); } } } }else { throw "FORM NOT EXISTS"; } }; /**** * JSON填充表 */ cpic.JSON2table = function(formId, json, formContext){ var form = null; if(formContext) { form = $(formId,formContext); }else { form= $(formId) } if(form) { for(var _i=0;_i<form.length;_i++){ if($(form[_i]).attr("name") && json[$(form[_i]).attr("name")]) { $(form[_i]).text(json[$(form[_i]).attr("name")]); }else if(!json[$(form[_i]).attr("name")] && $(form[_i]).attr("name")){ $(form[_i]).text(""); } } } }; /**** * 根据json属性清空表 */ cpic.initializeTable = function(formId, json, formContext){ var form = null; if(formContext) { form = $("#" + formId,formContext)[0]; }else { form= $(formId) } if(form) { for(var _i=0;_i<form.length;_i++){ if($(form[_i]).attr("name")) { $(form[_i]).html(""); } } } };
标签:idMap,insuredName,applicant,表单,JSON,var,applicantName,JS,applicantSex 来源: https://www.cnblogs.com/jiangaihu/p/14480765.html