js复选框的全选和取消全选的实现

这个博客展示了如何用JavaScript实现一个全选和反选的功能,通过监听复选框的点击事件来同步所有子项的选择状态。博客中包含了初始版本、简化版和最终完善的代码示例,确保在用户取消任何子项选择时,全选复选框也会相应更新状态。
摘要由CSDN通过智能技术生成

js实现:

<!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>Document</title>
</head>
<body>
    <script type="text/javascript">
        window.onload = function(){
            var quan = document.getElementById("quan");
            quan.onclick = function(){
                var aihaos = document.getElementsByName("aihao");
                for(var i = 0; i<aihaos.length ; i++){
                    if(quan.checked){
                        aihaos[i].checked = true;
                    }else{
                        aihaos[i].checked = false;
                    }
                }
            }
        }
    </script>
    <input type="checkbox" id="quan"/>
    <br>
    <input type="checkbox" name="aihao" value="smoke"/>抽烟
    <br>
    <input type="checkbox" name="aihao" value="drink"/>喝酒
    <br>
    <input type="checkbox" name="aihao" value="firehair"/>烫头
    <br>
</body>
</html>

简化版:

<!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>Document</title>
</head>
<body>
    <script type="text/javascript">
        window.onload = function(){
            var quan = document.getElementById("quan");
            quan.onclick = function(){
                var aihaos = document.getElementsByName("aihao");
                for(var i = 0; i<aihaos.length ; i++){
                    aihaos[i].checked = quan.checked;
                }
            }
        }
    </script>
    <input type="checkbox" id="quan"/>
    <br>
    <input type="checkbox" name="aihao" value="smoke"/>抽烟
    <br>
    <input type="checkbox" name="aihao" value="drink"/>喝酒
    <br>
    <input type="checkbox" name="aihao" value="firehair"/>烫头
    <br>
</body>
</html>

上面两种都有错误,全部选中后,取消下面任意一个,上面的全选还会打钩,下面是最终的完善版本:

<!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>Document</title>
</head>
<body>
    <script type="text/javascript">
        window.onload = function(){
            var quan = document.getElementById("quan");
            var aihaos = document.getElementsByName("aihao");
            quan.onclick = function(){
                for(var i = 0; i<aihaos.length ; i++){
                    aihaos[i].checked = quan.checked;
                }
            }

            for(var i = 0; i<aihaos.length ; i++){
                aihaos[i].onclick = function(){
                     var count = aihaos.length;
                     var checkedCount = 0;
                     for(var i = 0; i<aihaos.length; i++){
                         if(aihaos[i].checked){
                             checkedCount++;
                         }
                     }
                     quan.checked = (count ==checkedCount);
                }
            }
        }
    </script>
    <input type="checkbox" id="quan"/>
    <br>
    <input type="checkbox" name="aihao" value="smoke"/>抽烟
    <br>
    <input type="checkbox" name="aihao" value="drink"/>喝酒
    <br>
    <input type="checkbox" name="aihao" value="firehair"/>烫头
    <br>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值