美文网首页
全选功能

全选功能

作者: jiaomeichen | 来源:发表于2018-03-19 19:48 被阅读0次
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>jquery实现全选非全选</title>
        <script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
        <style>
            input {vertical-align: middle}
               .container {width: 500px;margin: 20px auto;}
        </style>
    </head>
    <body>
        <div class="container">
            <div class="top">
                <input type="checkbox" value="全选" id="all" class="all" />
                <label for="all">全选</label>
            </div>
            <div class="down">
                <div class="con">
                    <input type="checkbox" id="sel1" class="sel sel1" />
                    <label>选项1</label>
                </div>
                <div class="con">
                    <input type="checkbox" id="sel2" class="sel sel2" />
                    <label>选项2</label>
                </div>
                <div class="con">
                    <input type="checkbox" id="sel3" class="sel sel3" />
                    <label>选项3</label>
                </div>
            </div>
        </div>
        <script>
            $(".all").on("click",function() {
                $(".sel").prop("checked", $(this).prop("checked"));
            });
            $(".sel").on("click",function() {
                var $sel = $(".sel");
                var b = true;
                for(var i = 0; i < $sel.length; i++) {
                    if($sel[i].checked == false) {
                        b = false;
                        break;
                    }
                }
                $(".all").prop("checked", b);
            })
        </script>
    </body>
</html>

相关文章

网友评论

      本文标题:全选功能

      本文链接:https://www.haomeiwen.com/subject/phfufftx.html