1. 程式人生 > >jquery的form序列化後轉json

jquery的form序列化後轉json

return ray str query ring .html body inf blog

//form序列化後 轉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);
}

//提交
Submit = function(){

if( confirm("確認提交嗎?") ){

//form表單序列化
var form = $(parent.mainFrame.form1).serializeArray();

var form = arrayToJson(form);

$.ajax({
type :‘post‘,
url :‘../‘,
data : {
form : form,
ID : $("#idCard").html()
},
async:false,
dataType : ‘josn‘,
success : function(data){

}
});

}
}

jquery的form序列化後轉json