PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

基于jQuery实现复选框的全选 全不选 反选功能

高洛峰
高洛峰 原创
2017-02-06 13:17:04 700浏览

本代码是在众多的jQuery复选框功能代码中精选出来的,本人项目中使用的代码,这里分享给大家。

jQuery代码:

        $(function(){ 
            $("#checkedAll").click(function(){ 
                $('[name=items]:checkbox').attr('checked',true); 
            }); 
            $("#checkedNo").click(function(){ 
                $('[name=items]:checkbox').attr('checked',false); 
            }); 
            $("#checkedRev").click(function(){ 
                $('[name=items]:checkbox').each(function(){ 
                    //$(this).attr('checked',!$(this).attr('checked')); 
                    this.checked = !this.checked; 
                }); 
            }); 
            $("#send").click(function(){ 
               var str = "你选中的是:\r\n"; 
               $('[name=items]:checkbox:checked').each(function(){ 
                   str += $(this).val()+"\r\n"; 
               }); 
                alert(str); 
            }); 
        });

HTML代码:

    你爱好的运动是?<br> 
    <input type="checkbox" name="items" value="足球"/>足球 
    <input type="checkbox" name="items" value="篮球"/>篮球 
    <input type="checkbox" name="items" value="羽毛球"/>羽毛球 
    <input type="checkbox" name="items" value="乒乓球"/>乒乓球<br> 
    <input type="button" id="checkedAll" value="全 选"/> 
    <input type="button" id="checkedNo" value="全不选"/> 
    <input type="button" id="checkedRev" value="反 选"/> 
    <input type="button" id="send" value="提 交"/>

小伙伴们使用起来是不是很方便,这也是本人千挑万选出来的,希望能对大家有所帮助。

更多基于jQuery实现复选框的全选 全不选 反选功能相关文章请关注PHP中文网!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。