自定义jQuery serializeArray()

时间:2014-04-17 18:43:10

标签: javascript jquery json

我有这个序列化数组函数:

$.fn.serializeObject = function () {
     var o = {};
     var a = this.serializeArray();
     $.each(a, function () {
         if (o[this.name] !== undefined) {
             if (!o[this.name].push) {
                 o[this.name] = [o[this.name]];
             }
             o[this.name].push(this.value || '');
         } else {
             o[this.name] = this.value || '';
         }
     });
     return o;
 };

并且通常提交表单会产生这样的输出。

{
    "acctType1": "individual",
    "compare_act1": "contains",
    "match_name_act1": "accountName",
    "text_act1": "",
    "acctType2": "individual",
    "compare_act2": "contains",
    "match_name_act2": "accountName",
    "text_act2": "",
    "transType1": "401kContribution",
    "compare_trans1": "contains",
    "match_name_trans1": "description",
    "text_trans1": "",
    "transType2": "401kContribution",
    "compare_trans2": "contains",
    "match_name_trans2": "description",
    "text_trans2": ""
}

我想回复一下:

custom = {
    acct: {
        "acctType1": "individual",
        "compare_act1": "contains",
        "match_name_act1": "accountName",
        "text_act1": "",
        "acctType2": "individual",
        "compare_act2": "contains",
        "match_name_act2": "accountName",
        "text_act2": ""
    },
    trans: {
        "transType1": "401kContribution",
        "compare_trans1": "contains",
        "match_name_trans1": "description",
        "text_trans1": "",
        "transType2": "401kContribution",
        "compare_trans2": "contains",
        "match_name_trans2": "description",
        "text_trans2": ""
    }

之前表单中的所有键都是单个JSON字符串,现在我希望它们有两个JSON(数组......不确定这个包含所有acct和one trans的几个)。< / p>

需要格式化此JSON字符串。我该怎么做?

0 个答案:

没有答案
相关问题