先將表單數值轉換成數組存儲,存儲成的格式為[{"name":"","value":""},.....}] 將formArray轉換成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); }