input的全选反选

<body>

<input type="checkbox" class="all">全选

<div class="box">

    <input type="checkbox" >吃饭

    <br>

    <input type="checkbox">睡觉

    <br>

    <input type="checkbox">打豆豆

</div>

<script>

    var box=document.querySelector('.box')

    var input=box.querySelectorAll('input')

    var all=document.querySelector('.all')

    all.onclick=function () {

        for(var i=0;i<input.length;i++){

            input[i].checked=this.checked

        }

    }

    var stat

    for (var i=0;i<input.length;i++){

        input[i].onclick=function () {

            for(var j=0;j<input.length;j++){

                stat=true

                if(input[j].checked==false){

                    stat=false

                }

            }

            all.checked=stat

        }

    }

</script>

</body>

我最近在自学js,但是在操作全选反选的时候,发现这个问题,就是我感觉我这个是正确的,但是运行中,发现全选和反选都没正确执行,希望大家帮我看看,谢谢大家


ABOUTYOU
浏览 403回答 2
2回答

拉丁的传说

&nbsp; &nbsp; &nbsp; &nbsp; var box=document.querySelector('.box')&nbsp; &nbsp; &nbsp; &nbsp; var input=box.querySelectorAll('input')&nbsp; &nbsp; &nbsp; &nbsp; var all=document.querySelector('.all')&nbsp; &nbsp; &nbsp; &nbsp; all.onclick=function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var i=0;i<input.length;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input[i].checked=this.checked&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for (var i=0;i<input.length;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input[i].onclick=function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var j=0;j<input.length;j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(input[j].checked==false){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; all.checked=false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; all.checked=true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript