时间:2020-11-09 17:50:39 | 栏目:JavaScript代码 | 点击:次
先将表单数值转换成数组存储,存储成的格式为[{"name":"","value":""},.....}]
var formArray = $("#form").serializeArray();
将formArray转换成Json格式
function arrayToJson(formArray){ var dataArray = {}; $.each(formArray,function(){ if(dataArray[this.name]){ if(!dataArray[this.name].push){ dataArray[this.name] = [dataArray[this.name]]; } dataArray[this.name].push(this.value || ''); }else{ dataArray[this.name] = this.value || ''; } }); return JSON.stringify(dataArray); }