Reputation: 2622
I have view that contains a table with the following structure:
<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>
Where ItemId
is the value contained in the checkbox, and the id
of each input is appended with the Id
of the current item. How can I iterate through the table above with jQuery and create a JSON array containing the values of the textboxes in each row where the checkboxes checked
value is set to true?
I would like the JSON object to look like this (if possible):
[
{ "ItemId": "1", "ExcelColumn": "A", "ExcelRowStart": "5", "ExcelRowEnd": "20" },
{ "ItemId": "2", "ExcelColumn": "B", "ExcelRowStart": "5", "ExcelRowEnd": "20" },
.
.
.
{ "ItemId": "n", "ExcelColumn": "Z", "ExcelRowStart": "5", "ExcelRowEnd": "20" }
]
Upvotes: 2
Views: 4217
Reputation: 46067
Here you go:
$("#mappings tr input:checkbox:checked")
.closest("tr").find("input:text").css({ "border" : "1px solid red" });
Here's a jsFiddle: http://jsfiddle.net/xgSpp/
To serialize the selection to JSON you can simply call serializeArray
. The only catch is that you have to provide names for the elements.
var result = $("#mappings tr input:checkbox:checked").closest("tr").find("input:text").serializeArray();
alert(JSON.stringify(result));
Upvotes: 7
Reputation: 3511
If you add class for the input, then coding can be simpler
<tr>
<td><input type="checkbox" id="cb-1" value="1" class="ItemId" /> Item 1</td>
<td><input type="text" id="col-1" value="A" class="ExcelColumn"/></td>
<td><input type="text" id="rstart-1" value="5" class="ExcelRowStart" /></td>
<td><input type="text" id="rend-1" value="20" class="ExcelRowEnd" /></td>
</tr>
var result = [];
$("tbody :checked").each(function(){
var tr = $(this).parent('td').parent('tr');
var item = {};
$("input", tr).each(function(){
item[$(this).attr("class")] = $(this).val();
});
result.push(item);
});
check this at http://jsfiddle.net/mxELP/4/
Upvotes: 3
Reputation: 10315
Alright so here's what I came up with: http://jsfiddle.net/manuel/rmwSY/
Mind that I think you made a typo on <td><input type="checkbox" id="cb-2" value="1" />
. Shouldn't this be value="2"
?
Upvotes: 0