下面小编就为大家带来一篇form表单转Json提交的方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
先将表单数值转换成数组存储,存储成的格式为[{"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); }