我想将复选框设置为检查表的前 2 个表行中的输入字段。
标记
<tbody>
<tr>
<td>
<input type="checkbox" id="" class="checkinput financeOption"><!--want this checked on page load-->
</td>
<td class="datatd">
<label id="">Option 1</label>
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="" class="checkinput financeOption"><!--want this checked on page load-->
</td>
</td>
<td class="datatd"><label id="">Option 2 </label>
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="" class="checkinput financeOption">
</td>
<td class="datatd"><label id="">Option 3</label>
</td>
</tr>
我知道我可以使用 $( "").prop('checked', true);设置属性,但很难弄清楚如何定位前 2 个属性。
谢谢
最佳答案
您可以使用例如nth-child(-n+2)
选择器:
$('table tr:nth-child(-n+2) :checkbox').prop('checked', true);
关于javascript - 将复选框设置为选中前 2 个表行中的输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24151395/