我的 View 包含具有以下结构的表:
<table id="mappings">
<thead>
<tr>
<th width="40%"><input type="checkbox" id="cb-master" /> Structure</th>
<th width="15%">Excel Column</th>
<th width="15%">Excel Row Start</th>
<th width="15%">Excel Row End</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" id="cb-1" value="1" /> Item 1</td>
<td><input type="text" id="col-1" value="A" /></td>
<td><input type="text" id="rstart-1" value="5" /></td>
<td><input type="text" id="rend-1" value="20" /></td>
</tr>
<tr>
<td><input type="checkbox" id="cb-2" value="2" /> Item 2</td>
<td><input type="text" id="col-2" value="B" /></td>
<td><input type="text" id="rstart-2" value="5" /></td>
<td><input type="text" id="rend-2" value="20" /></td>
</tr>
.
.
.
<tr>
<td><input type="checkbox" id="cb-n" value="n" /> Item n</td>
<td><input type="text" id="col-n" value="Z" /></td>
<td><input type="text" id="rstart-n" value="5" /></td>
<td><input type="text" id="rend-n" value="20" /></td>
</tr>
</tbody>
</table>
其中 ItemId
是复选框中包含的值,每个输入的 id
附加有当前项目的 Id
。如何使用 jQuery 遍历上表并创建一个 JSON 数组,其中包含每行中复选框 checked
值设置为 true 的文本框的值?
我希望 JSON 对象看起来像这样(如果可能的话):
[
{ "ItemId": "1", "ExcelColumn": "A", "ExcelRowStart": "5", "ExcelRowEnd": "20" },
{ "ItemId": "2", "ExcelColumn": "B", "ExcelRowStart": "5", "ExcelRowEnd": "20" },
.
.
.
{ "ItemId": "n", "ExcelColumn": "Z", "ExcelRowStart": "5", "ExcelRowEnd": "20" }
]
最佳答案
给你:
$("#mappings tr input:checkbox:checked")
.closest("tr").find("input:text").css({ "border" : "1px solid red" });
这是一个 jsFiddle:http://jsfiddle.net/xgSpp/
要将选择序列化为 JSON,您只需调用 serializeArray
即可。唯一的问题是您必须提供元素的名称。
var result = $("#mappings tr input:checkbox:checked").closest("tr").find("input:text").serializeArray();
alert(JSON.stringify(result));
关于javascript - jQuery 选择选中复选框的每一行中的所有文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8170318/