假设我有下表:
<table>
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>A1</td>
<td><input type="checkbox"/> A2</td>
<td>A3</td>
</tr>
<tr>
<td>B1</td>
<td><input type="checkbox"/> B2</td>
<td>B3</td>
</tr>
<tr>
<td>C1</td>
<td><input type="checkbox"/> C2</td>
<td>C3</td>
</tr>
</tbody>
</table>
我只想从表中为选中复选框的行生成 JSON。我为“在此处插入支票”输入了什么?
var myRows = [];
var $headers = $("th");
var $rows = $("tbody tr").each(function(index) {
$cells = $(this).find("td");
myRows[index] = {};
$cells.each(function(cellIndex) {
// Insert Check Here
myRows[index][$($headers[cellIndex]).html()] = $(this).html();
});
});
var myObj = {};
myObj.myrows = myRows;
alert(JSON.stringify(myObj));