jquery全选和反选

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="http://lib.sinaapp.com/js/jquery/2.0/jquery.min.js"></script>
</head>
<body>
    <div>
        <input type="checkbox" class="all-box" />全选
    </div>
    <div>
        <input type="checkbox" class="chd-box"/>手机
    </div>
    <div>
        <input type="checkbox" class="chd-box" />电脑
    </div>
    <div>
        <input type="checkbox" class="chd-box" />平板
    </div>
    <div>
        <input type="checkbox" class="chd-box" />内存卡
    </div>
    <div>
        <input type="checkbox" class="chd-box" />影碟机
    </div>
    <script>
        $(".all-box").change(e=>$(".chd-box").prop('checked',e.target.checked));
        $(".chd-box").change(function () {
            var all = $(".chd-box").length;
            var sel = $(".chd-box:checked").length;
            $(".all-box").prop('checked',all==sel);
        })
    </script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qiankui/article/details/82897560