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" }
]
If you add class for the input, then coding can be simpler
check this at http://jsfiddle.net/mxELP/4/