简体   繁体   English

选择仅第一列复选框jquery

[英]select Only first column checkbox jquery

I am trying to select first column of simple table. 我试图选择简单表的第一列。 here is my code but it select all checkbox appear in table. 这是我的代码,但是它选中了所有出现在表格中的复选框。 how to avoid it. 如何避免。

Html HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<table border="1" width="
300">
    <tr>
        <th>
            <input type="checkbox" id="selectAll" />
        </th>
        <th colspan="2">select all!</th>
    </tr>
    <tr>
        <td><input type="checkbox" id="1"/> </td>
        <td>test!</td>
        <td>hi!</td>
    </tr>

     <tr>
        <td><input type="checkbox" id="1"/> </td>
        <td>test!</td>
        <td>hi!</td>
    </tr>



     <tr>
        <td><input type="checkbox" id="1"/> </td>
        <td>test!</td>
        <td><input type="checkbox" id="1"/></td>
    </tr>

</table>

JQuery jQuery查询

<script>


    $('#selectAll').click(function(e){
        var table= $(e.target).closest('table');
        $('td input:checkbox',table).prop('checked',this.checked);
    });
    </script>

Expected Output 预期产量

输出

Here all checkbox selected in table but i want to select only first column checkbox in table. 这里所有在表中选中的复选框,但我只想在表中选择第一列复选框。 please suggest me better way 请给我建议更好的方法

Change the checkbox selector to only select the checkboxes from the first td in a row: td:first-child . 更改复选框选择器以仅选择连续第一个td中的复选框: td:first-child

 $('#selectAll').click(function(e) { var table = $(e.target).closest('table'); $('td:first-child input:checkbox', table).prop('checked', this.checked); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <table border="1" width=" 300"> <tr> <th> <input type="checkbox" id="selectAll" /> </th> <th colspan="2">select all!</th> </tr> <tr> <td><input type="checkbox" id="1" /> </td> <td>test!</td> <td>hi!</td> </tr> <tr> <td><input type="checkbox" id="1" /> </td> <td>test!</td> <td>hi!</td> </tr> <tr> <td><input type="checkbox" id="1" /> </td> <td>test!</td> <td><input type="checkbox" id="1" /></td> </tr> </table> 

You need to apply change on first td of every row. 您需要在每行的第一个td上应用更改。

Here is working code 这是工作代码

 $('#selectAll').click(function(e){ $(e.target).closest('table').find("tr td:first-child input:checkbox").prop('checked',this.checked) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table border="1" width=" 300" > <tr> <th> <input type="checkbox" id="selectAll" /> </th> <th colspan="2">select all!</th> </tr> <tr> <td> <input type="checkbox" id="1" /> </td> <td>test!</td> <td>hi!</td> </tr> <tr> <td> <input type="checkbox" id="1" /> </td> <td>test!</td> <td>hi!</td> </tr> <tr> <td> <input type="checkbox" id="1" /> </td> <td>test!</td> <td> <input type="checkbox" id="1" /> </td> </tr> </table> 

Use below. 在下面使用。 It selects first td under each tr and then selects checkbox under it. 它首先在每个tr下选择td,然后选择其下的复选框。

 $('tr td:first-child input[type=checkbox]',table).prop('checked',this.checked);

You want to use td:first-child 您想使用td:first-child

  $('#selectAll').click(function(e){ var table= $(e.target).closest('table'); table.find('td:first-child input:checkbox').prop('checked',this.checked); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table border="1" width="300"> <tr> <th> <input type="checkbox" id="selectAll" /> </th> <th colspan="2">select all!</th> </tr> <tr> <td><input type="checkbox" class="firstRow"/> </td> <td>test!</td> <td>hi!</td> </tr> <tr> <td><input type="checkbox" class="firstRow"/> </td> <td>test!</td> <td>hi!</td> </tr> <tr> <td><input type="checkbox" class="firstRow"/> </td> <td>test!</td> <td><input type="checkbox" id="1"/></td> </tr> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM