jQuery 全选跟单选功能

jQuery 全选跟单选功能

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h2>管理员列表</h2>
    <table border="1px" width="500px">
        <thead>
            <tr>
                <th><input type="checkbox" class="all"/>全选</th>
                <th>管理员ID</th>
                <th>姓名</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input type="checkbox" class="it"/></td>
                <td>1</td>
                <td>Tester</td>
                <td>修改 删除</td>
            </tr>
            <tr>
                <td><input type="checkbox" class="it"/></td>
                <td>2</td>
                <td>Manager</td>
                <td>修改 删除</td>
            </tr>
            <tr>
                <td><input type="checkbox" class="it"/></td>
                <td>3</td>
                <td>Analyst</td>
                <td>修改 删除</td>
            </tr>
            <tr>
                <td><input type="checkbox" class="it"/></td>
                <td>4</td>
                <td>Admin</td>
                <td>修改 删除</td>
            </tr>
        </tbody>
    </table>
    <script src="./jquery-3.4.1.js"></script>

    <script>
        $(".all").click(function(){
            if($(this).is(":checked")){
                // console.log(this.checked)
                $(".it").prop("checked",true);
            }else{
                // console.log(this.checked)
                $(".it").prop("checked",false);
            }
        })
    
        $("tbody input[type='checkbox']").click(function(){
            $("tbody input[type='checkbox']").each(function(){
                if($(this).is(":checked")==false){
                    // console.log(1)
                    $(".all").prop("checked",false);
                }else{
                    var unchecked = $("tbody>tr>td:first-child>input:not(:checked)")
                    // console.log(unchecked)
                    if(unchecked.length==0){
                        $(".all").prop("checked",true);
                    }else{
                        $(".all").prop("checked",false);
                    }
                }
            })
        })
        // $("tbody input[type='checkbox']").each(function(){
        //     if($(this).is(":checked")==false){
        //         $(".all").prop("checked",false);
        //     }else{
        //         var unchecked = $("tbody>tr>td:first-child>input:not(:checked)")
        //         if(unchecked==null){
        //             $(".all").prop("checked",true);
        //         }else{
        //             $(".all").prop("checked",false);
        //         }
        //     }
        // })
    </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值