JavaScript反选-全选-全不选代码

来源:互联网 发布:牛菲特软件 编辑:程序博客网 时间:2024/06/06 08:43

<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="JS/jquery-1.12.2.js"></script>


</head>

<body>

//打出选项

<input type="checkbox"/>香蕉

<input type="checkbox"/>苹果
<input type="checkbox"/>草莓
<input type="checkbox"/>葡萄
<input type="checkbox"/>榴莲
<input type="checkbox"/>菠萝蜜
<br/><br/>
<input type="button" id="btnall" value="全选"/>
<input type="button" id="btnno" value="全不选"/>

<input type="button" id="btnreverse" value="反选"/>

<script>

//全选
    $(function(){
        $("#btnall").click(function(){
            $("input[type='checkbox']").each(function(i){
               this.checked=true;
            });
            //对于复选框来说,使用attr来设置属性,会有问题,所以换用了prop;
            $("input[type='checkbox']").prop("checked", true);
        });
//全不选       

 $("#btnno").click(function(){
            $("input[type='checkbox']").prop("checked", false);
        });

// 反选
        $("#btnreverse").click(function(){
            $("input[type='checkbox']").each(function(i){
                this.checked=!this.checked;
            });
        });


    });
</script>
</body>
</html>



0 0