将前2行输入字段的复选框设置为选中

Set the checkbox to checked for input fields in the first 2 table rows

本文关键字:设置 复选框 2行 输入 字段 将前      更新时间:2023-09-26

我想将复选框设置为选中表的前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>

我知道我可以使用$(")道具("检查",真正的);设置属性,但挣扎于弄清楚如何瞄准前2。

谢谢

您可以使用例如nth-child(-n+2)选择器:

$('table tr:nth-child(-n+2) :checkbox').prop('checked', true);
http://jsfiddle.net/B8h7N/

直接切开:

$("[type=checkbox]").slice(0,2).prop('checked', true);