当前位置:Gxlcms > JavaScript > 基于jQuery实现复选框的全选全不选反选功能

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

时间:2021-07-01 10:21:17 帮助过:7人阅读

本代码是在众多的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中文网!

人气教程排行