次の表があると仮定します。
<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));