jquery全选 选中所有checkbox,再次点击取消全选,可以反复点击,解决了点击一次后再次点击不能选中的问题

<!DOCTYPE html>
<html>
<head>
    <title>checkbox的多选</title>
</head>
<body>
     <table border="1" id="table11" width="350px" height="200px" align="center">
         <tr>
             <th>选项</th>
             <th>编号</th>
             <th>姓名</th>
             <th>性别</th>
             <th>薪资</th>
         </tr>
        <tr id="0">
            <td><input type="checkbox" id="Checkbox1" name="checkbox" value="0"></td>
            <td>1001</td>
            <td>李晓明</td>
            <td>男</td>
            <td>3650(元)</td>
        </tr>
         <tr id="0">
            <td><input type="checkbox" id="Checkbox1" name="checkbox" value="0"></td>
            <td>1001</td>
            <td>李晓明</td>
            <td>男</td>
            <td>3650(元)</td>
        </tr>
         <tr id="0">
            <td><input type="checkbox" id="Checkbox1" name="checkbox" value="0"></td>
            <td>1001</td>
            <td>李晓明</td>
            <td>男</td>
            <td>3650(元)</td>
        </tr>
         <tr id="0">
            <td><input type="checkbox" id="Checkbox1" name="checkbox" value="0"></td>
            <td>1001</td>
            <td>李晓明</td>
            <td>男</td>
            <td>3650(元)</td>
        </tr>
     </table>
     <table border="1" align="center">
         <tr>
             <td style="height: 20px;width:340px">
                 <span><input type="checkbox" name="" id="chkAll">全选</span>&nbsp;&nbsp;&nbsp;&nbsp;
                 <!--<span><input type="checkbox" name="" id="chkAll1">取消</span>-->
             </td>
         </tr>
     </table>
     <script type="text/javascript" src="jquery-3.3.1.js"></script>
     <script type="text/javascript">
         $(function(){
             $("table tr:nth-child(odd)").css("background-color","#eee");//奇数行变色
             //全选点击函数
             $("#chkAll").click(function(){
                 if(this.checked){//如果自己被选中
                     //$("input[type=checkbox]").prop("checked", "true");
                     $("input[name=checkbox]").prop("checked", "true");
                 }
                 else{
                     //$("input[name=checkbox]").removeAttr("checked");失效
                     //$("input[name=checkbox]").prop("checked", "false");失效
                     //$("input[value='全选']").on('click',function(){this.checked = false;}) 失效
                     $("input[name='checkbox']").each(function(){
                              this.checked=false;
                              });
                 }
             });
             
         });
     </script>
</body>
</html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值