js全选反选

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <div>
            <input type="checkbox" name="checkboxs" id="" value="咕咕" />咕咕<br>
            <input type="checkbox" name="checkboxs" id="" value="嘎嘎" />嘎嘎<br>
            <input type="checkbox" name="checkboxs" id="" value="嘟嘟" />嘟嘟<br>
            <input type="button" id="quanxuan" value="全选" />
            <input type="button" id="quanbuxuan" value="全不选" />
            <input type="button" id="fanxuan" value="反选" />
        </div>
        <script type="text/javascript">
            var checkboxs = document.getElementsByName("checkboxs");
            
            document.getElementById("quanxuan").onclick=function(){
                for(var i=0;i<checkboxs.length;i++){
                    checkboxs[i].checked = true;
                }
            }
            document.getElementById("quanbuxuan").onclick=function(){
                for(var i=0;i<checkboxs.length;i++){
                    checkboxs[i].checked = false;
                }
            }
            document.getElementById("fanxuan").onclick=function(){
                for(var i=0;i<checkboxs.length;i++){
                    checkboxs[i].checked = !checkboxs[i].checked;
                }
            }
            
        </script>
    </body>
</html>

猜你喜欢

转载自www.cnblogs.com/qilin20/p/12655447.html