序列化多个form表单内容同时提交

时间:2024-10-07 14:37:20

一、首先将表单主体序列化为json对象。

方法:

 //将表单序列化为json,这里加了个jQuery的扩展方法
$.fn.serializeJson = function () {
var result = {};
var array = this.serializeArray();
$(array).each(function () {
if (result[this.name]) {
if ($.isArray(result[this.name])) {
result[this.name].push(this.value);
} else {
result[this.name] = [result[this.name], this.value];
}
} else {
result[this.name] = this.value;
}
});
return result;
};

使用方法为:var jsonData=$("#form").serializeJson();

二、序列化form数据

f.GetFormData = function (formId, dataFailed, jsonData) {
var array = $("#" + formId).serializeArray();
$(array).each(function (i, o) {
var n = o.name, v = o.value;
jsonData[dataFailed + "." + n] = v;
});
return jsonData;
}

三、序列化grid数据

 

f.GetGridData = function (gridname, datafailed, jsondata) {

        var grid = $("#" + gridname);
var addList = grid.datagrid("getChanges");
$.each(addList, function (i, o) {
o.InfoGroupName = gridname;
for (var p in o) {
jsondata[datafailed + "[" + i + "]." + p] = addList[i][p];
}
});
return jsondata;
}