shu*_*iar 2 javascript jquery asp.net-mvc-3
我有一个包含以下结构的表的视图:
<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>
Run Code Online (Sandbox Code Playgroud)
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" }
]
Run Code Online (Sandbox Code Playgroud)
干得好:
$("#mappings tr input:checkbox:checked")
.closest("tr").find("input:text").css({ "border" : "1px solid red" });
Run Code Online (Sandbox Code Playgroud)
这是一个jsFiddle:http://jsfiddle.net/xgSpp/
要将选择序列化为JSON,您只需调用即可serializeArray
.唯一的问题是你必须提供元素的名称.
var result = $("#mappings tr input:checkbox:checked").closest("tr").find("input:text").serializeArray();
alert(JSON.stringify(result));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4145 次 |
最近记录: |