我有一個具有類似輸入元素組的HTML表單。當序列化表單數據時保留分組
如何在保留分組的同時序列化表單數據,而不迭代輸入元素?
如果有必要實現目標,我可以改變表單結構。
示例輸入:
<form id="form">
<div>
<input name="id" value="1" type="hidden">
<input name="quantity" value="10">
</div>
<div>
<input name="id" value="2" type="hidden">
<input name="quantity" value="20">
</div>
<div>
<input name="id" value="3" type="hidden">
<input name="quantity" value="30">
</div>
</form>
預期輸出:
{
"1": { "id": "1","quantity": "10" },
"2": { "id": "2","quantity": "20" },
"3": { "id": "3","quantity": "30" }
}
我嘗試:
console.log($('#form').serializeArray());
但形式數據沒有被分組:
[
{"name":"id","value":"1"},{"name":"quantity","value":"10"},
{"name":"id","value":"2"},{"name":"quantity","value":"20"},
{"name":"id","value":"3"},{"name":"quantity","value":"30"}
]