这是我的代码序列化表单的当前代码。
$.fn.serializeObject = function () {
var o = {};
var a = this.serializeArray();
console.log("A:"+a);
$.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": ""
}
但是我想这样做:
{
"acctobj": {
"acctType1": "individual",
"compare_act1": "contains",
"match_name_act1": "accountName",
"text_act1": "",
"acctType2": "individual",
"compare_act2": "contains",
"match_name_act2": "accountName",
"text_act2": ""
},
"transobj": {
"transType1": "401kContribution",
"compare_trans1": "contains",
"match_name_trans1": "description",
"text_trans1": "",
"transType2": "401kContribution",
"compare_trans2": "contains",
"match_name_trans2": "description",
"text_trans2": ""
}
}
所以基本上我必须进入acct{}
直到搜索遇到trans,然后将其推送到trans
$.fn.serializeObject = function () {
var o = {};
var acctobj={};
var transobj={};
var a = this.serializeArray();
$.each(a, function () {
if (acctobj[this.name.search("trans")] =-1 ) {
if (!acctobj[this.name].push) {
acctobj[this.name] = [acctobj[this.name]];
}
acctobj[this.name].push(this.value || '');
} else {
transobj[this.name] = this.value || '';
}
});
return o;
};
搜索var a
并继续推进acctobj
,直至找到"trans"
,然后推入transobj
然后在acctobj
transobj
和var o={}
提前致谢。