我有下一个表格:
<form id="form">
<input type="checkbox" name="numbers" value="1">
<select name="ordering[numbers]">
<option value="ASC">Ascend</option>
<option value="DESC">Descend</option>
</select><br>
<input type="checkbox" name="added_date" value="1"> Call date
<select name="ordering[added_date]">
<option value="ASC">Ascend</option>
<option value="DESC">Descend</option>
</select>
</form>
基本上我需要在提交表单之前将所有数据保存到 JSON 对象,我们称之为 form_json 我正在使用 jQuery.serializeArray() 函数
var form_json = $("#form").serializeArray();
如果两个复选框都被选中,我会得到下一个对象:
[
{"name": "numbers", "value": "1"},
{"name": "ordering[numbers]", "value": "ASC"},
{"name": "added_date","value":"1"},
{"name": "ordering[added_date]", "value": "ASC"}
]
这不是我所期望的。我想以某种方式获得下一种格式:
[
{"name": "numbers","value": "1"},
{"name": "added_date","value": "1"},
{"name": "ordering", "value": {
"numbers": "ASC",
"added_date": "ASC"}
}
]
有可能这样做吗?谢谢!