表单全选与取消选择

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表单全选与取消选择</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
        integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <style>
        #formChoose {
            font-size: 18px;
            font-family: '宋体';
            margin-top: 50px;
        }

        h2 {
            text-align: center;
            color: lightblue;
        }

        h2:hover {
            color: pink;
        }

        th {
            background-color: azure;
        }

        table,
        th {
            text-align: center;
        }
        /* table悬浮整行 */
        /* tbody tr:hover td{cursor:pointer;background-color: antiquewhite;} */
       .q:hover{cursor:pointer;background-color: antiquewhite;}
    </style>
</head>

<body>
    <div id="formChoose">
        <h2>表单全选与取消选择</h2>
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>
                        <input type="checkbox" name="全选" id="allChoose">
                    </th>
                    <th>姓名</th>
                    <th>职位</th>
                    <th>部门</th>
                </tr>
            </thead>
            <tbody>
                <tr class="q">
                    <td>
                        <input type="checkbox" name="单选">
                    </td>
                    <td>小白</td>
                    <td>Web前端实习</td>
                    <td>综合部</td>
                </tr>
                <tr class="q">
                    <td>
                        <input type="checkbox" name="单选">
                    </td>
                    <td>小柒</td>
                    <td>Java开发</td>
                    <td>综合部</td>
                </tr>
                <tr class="q">
                    <td>
                        <input type="checkbox" name="单选">
                    </td>
                    <td>小寒</td>
                    <td>HR</td>
                    <td>人事部</td>
                </tr>
            </tbody>
        </table>
    </div>
    <script>
        //表单全选、取消全选,循环让checked属性跟随复选框即可
        var all = document.getElementById('allChoose')
        var tbInput = document.querySelector('tbody').getElementsByTagName('input')
        
        all.onclick = function(){
            //console.log(this.checked)  输出true或false
            for(i=0;i<tbInput.length;i++){
                tbInput[i].checked = this.checked;
            }
        }

        //为下面的小复选框注册事件,当全部选中时,总复选框也选中,当其中有一个么选中时,复选框取消选中
        for(i=0;i<tbInput.length;i++){
        tbInput[i].onclick = function(){
               var flag = true; // flag控制全选按钮是否选中
               //检查所有的小复选框的选中状态 
                for (var i=0;i<tbInput.length;i++){
                   if(!tbInput[i].checked){  // 如果有一个么有选中,flag=false,且break退出
                        flag = false;
                        break;
                    }
                }
                all.checked = flag;
            }
        }
    </script>
</body>

</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值