首页 > 代码库 > 序列化多个form表单内容同时提交

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

 

一、首先将表单主体序列化为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=http://www.mamicode.com/$("#form").serializeJson();

二、序列化form数据

f.GetFormData = http://www.mamicode.com/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 = http://www.mamicode.com/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;
    }

 

 

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