关于全选和全部选及个别操作

本文介绍了一个简单的全选复选框功能实现方式,并通过querySelectorAll选择器来判断是否所有选项都被选中。当所有选项被选中时,全选复选框也会被选中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<body>
    <label for="nice1" class="labels"><input type="checkbox" id="nice1" name="nice" class="hello">选项一</label>
    <label for="nice2" class="labels"><input type="checkbox" id="nice2" name="nice" class="hello">选项二</label>
    <label for="nice3" class="labels"><input type="checkbox" id="nice3" name="nice" class="hello">选项三</label>
    <label for="nice4" class="labels"><input type="checkbox" id="nice4" name="nice" class="hello">选项四</label>
    <label for="nice5" class="labels"><input type="checkbox" id="nice5" name="nice" class="hello">选项五</label>
    <label for="nice6" class="labels"><input type="checkbox" id="nice6" name="nice" class="hello">选项六</label>
    <label for="nice7" class="labels"><input type="checkbox" id="nice7" name="nice" class="hello">选项七</label>
    <label for="nice8" class="labels"><input type="checkbox" id="nice8" name="nice" class="hello">选项八</label>
    <label for="nice9" class="labels"><input type="checkbox" id="nice9" name="nice" class="hello">选项九</label>
    <label for="nice10" class="labels"><input type="checkbox" id="nice10" name="nice" class="hello">选项十</label>
    <label for="all_c" class="all"><input type="checkbox" id="all_c" class="all_c">全选</label>
<script>
    $(function(){
        $('.hello').click(function(){
            var cel=document.querySelectorAll('input[name="nice"]:checked');
            console.log(cel.length);
            if(cel.length==$('.hello').length){
                $('.all_c').prop('checked',true);
            }else{
                $('.all_c').prop('checked',false);
            }
        });
        $('.all_c').click(function(){
            if($(this).is(":checked")){
                $('.hello').prop('checked',true);
            }else{
                $('.hello').prop('checked',false);
            }
        });
    })
</script>

发现这个querySelectorAll()挺好用的,但是好像兼容不是很好,不过可以学习一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值