javascript使用serializeObject()序列化具有嵌套对象值的窗体



我问如何在javascript中用嵌套对象序列化object?这个问题早些时候,我认为我几乎用Object.values得到了答案,但我仍然无法解决它,所以我改变了我的问题,再次提问。

我有

$.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;
};

作为serializeObject((;并形成

var resourceRatioBoxTag = new Array();
for (var i = 0; i < selectedData.resource_ratio.length; i++) {
resourceRatioBoxTag[i] = "<p id='resourceRatio[" + i + "]' name='resourceRatio'>";
for (var j = 0; j < selectedData.resource_ratio[i].length; j++) {
resourceRatioBoxTag[i] += "<input type='text' id='resourceRatio[" + i + "][" + j + "]' value='" + selectedData.resource_ratio[i][j] + "' name='resourceRatio[" + i + "]'>";
}
resourceRatioBoxTag[i] += "</p>";
$("#resourceRatioDiv").append(resourceRatioBoxTag[i]);
}

所以基本上我的HTML看起来是这样的:

<p id="resourceRatio[0]" name="resourceRatio">
<input type="text" id="resourceRatio[0][0]" value="Barbara" name="resourceRatio[0]">
<input type="text" id="resourceRatio[0][1]" value="Ben" name="resourceRatio[0]">
<input type="text" id="resourceRatio[0][2]" value="Anne" name="resourceRatio[0]">
<input type="text" id="resourceRatio[0][3]" value="John" name="resourceRatio[0]">
<input type="text" id="resourceRatio[0][4]" value="Cindy" name="resourceRatio[0]">
<input type="text" id="resourceRatio[0][5]" value="Nick" name="resourceRatio[0]">
<input type="text" id="resourceRatio[0][6]" value="Lex" name="resourceRatio[0]">
<input type="text" id="resourceRatio[0][7]" value="Edd" name="resourceRatio[0]">
<input type="text" id="resourceRatio[0][8]" value="Eric" name="resourceRatio[0]">
<input type="text" id="resourceRatio[0][9]" value="Jacky" name="resourceRatio[0]">
<input type="text" id="resourceRatio[0][10]" value="Paul" name="resourceRatio[0]">
</p>
<p id="resourceRatio[1]" name="resourceRatio">
<input type="text" id="resourceRatio[1][0]" value="0.11974110032362459" name="resourceRatio[1]">
<input type="text" id="resourceRatio[1][1]" value="0.037756202804746494" name="resourceRatio[1]">
<input type="text" id="resourceRatio[1][2]" value="0.23516720604099245" name="resourceRatio[1]">
<input type="text" id="resourceRatio[1][3]" value="0.10895361380798274" name="resourceRatio[1]">
<input type="text" id="resourceRatio[1][4]" value="0.10140237324703344" name="resourceRatio[1]">
<input type="text" id="resourceRatio[1][5]" value="0.03559870550161812" name="resourceRatio[1]">
<input type="text" id="resourceRatio[1][6]" value="0.02912621359223301" name="resourceRatio[1]">
<input type="text" id="resourceRatio[1][7]" value="0.08737864077669903" name="resourceRatio[1]">
<input type="text" id="resourceRatio[1][8]" value="0.02481121898597627" name="resourceRatio[1]">
<input type="text" id="resourceRatio[1][9]" value="0.1186623516720604" name="resourceRatio[1]">
<input type="text" id="resourceRatio[1][10]" value="0.10140237324703344" name="resourceRatio[1]"></p>

我想获得json对象的序列化结果为:

"resource_ratio": [
[
"Barbara",
"Ben",
"Anne",
"John",
"Cindy",
"Nick",
"Lex",
"Edd",
"Eric",
"Jacky",
"Paul"
],
[
0.11974110032362459,
0.037756202804746494,
0.23516720604099245,
0.10895361380798274,
0.10140237324703344,
0.03559870550161812,
0.02912621359223301,
0.08737864077669903,
0.02481121898597627,
0.1186623516720604,
0.10140237324703344
]
]

但以我目前的代码,我得到

{
"resourceRatio[0]": [
"Barbara",
"Ben",
"Anne",
"John",
"Cindy",
"Nick",
"Lex",
"Edd",
"Eric",
"Jacky",
"Paul"
],
"resourceRatio[1]": [
"0.11974110032362459",
"0.037756202804746494",
"0.23516720604099245",
"0.10895361380798274",
"0.10140237324703344",
"0.03559870550161812",
"0.02912621359223301",
"0.08737864077669903",
"0.02481121898597627",
"0.1186623516720604",
"0.10140237324703344"
]
}

我想在更改serializeObject((函数以帮助我获得所需的结果,或者更改输入标记(例如,输入标记的名称(以获得序列化的嵌套对象结果方面获得帮助。有人能帮帮我吗?

p.s:如果我将输入标记的名称从resourceRatio[+I+]更改为resourceRatio,它只返回1个数组[val[0][0]。val[1][10]],因此不能使用:(

尝试

var  serializedData = data.serializeObject();
var obj = Object.values(serializedData);

它将只为您提供serializedData{key:value}表单值的返回值。(在您的情况下,[names][numbers](因为它只复制您的值,所以您不必关心您想给resource_ratio标签取什么名字。因为您想要的结果不是[0:[],1:[],而是[[],[]]

最新更新