jQuery-常见的全选按钮和单选按钮同步

  • 下面是项目的结构代码,需要拷贝使用注意引入要 jquery
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>全选</title>
    <script src="./jquery-1.11.1.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .wrap {
            width: 300px;
            margin: 100px auto 0;
        }

        table {
            border-collapse: collapse;
            border-spacing: 0;
            border: 1px solid #c0c0c0;
        }

        th,
        td {
            border: 1px solid #d0d0d0;
            color: #404060;
            padding: 10px;
        }

        th {
            background-color: #09c;
            font: bold 16px "微软雅黑";
            color: #fff;
        }

        td {
            font: 14px "微软雅黑";
        }

        tbody tr {
            background-color: #f0f0f0;
        }

        tbody tr:hover {
            cursor: pointer;
            background-color: #fafafa;
        }
    </style>

</head>

<body>
    <div class="wrap">
        <table>
            <thead>
                <tr>
                    <th>
                        <input type="checkbox" id="j_cbAll" />
                    </th>
                    <th>课程名称</th>
                    <th>所属学院</th>
                </tr>
            </thead>
            <tbody id="j_tb">
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>JavaScript</td>
                    <td>前端与移动开发学院</td>
                </tr>

                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>css</td>
                    <td>前端与移动开发学院</td>
                </tr>

                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>html</td>
                    <td>前端与移动开发学院</td>
                </tr>

                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>jQuery</td>
                    <td>前端与移动开发学院</td>
                </tr>
            </tbody>
        </table>
    </div>
</body>

</html>
  • 下面是js的实现的基本逻辑和实现代码
<script>
        // 案例要求
        // 1)点击上面的全选,下面全部选中。
        // 2)下面的input如果全部选中,上面的input同步选中。
        $(function () {
            // 1. 全选按钮被点击,其他的按钮也被选中:被选中就是(checked="checked")
            $('#j_cbAll').click(function () {
                // 获取元素本身的属性使用prop()
                // 将全选按钮的的变化赋值给单个按钮
                var bool = $('#j_cbAll').prop('checked')
                $('#j_tb input').prop('checked', bool)
            })
            // 2. 再将单个按钮变化赋值影响全选按钮
            // 将小按钮的数量长度和被选中的按钮的数量长度比较的结果赋值全选按钮
            $('#j_tb input').click(function () {
                var n = $('#j_tb input:checked').length;
                var m = $('#j_tb input').length;
                var bool = (n === m)
                $('#j_cbAll').prop('checked', bool)
            })
        })
    </script>
  • 总结: 像这种全选和单选按钮同步的问题的思路分成两个步骤
    1. 就是将全选(input)按钮的(checked的属性)赋值变化赋值给那些单选按钮:全选按钮被选中状态,和其他单独按钮 被选中状态一致
    2. 将单独按钮的数量和被选中的单独按钮的数量比较,得到一个布尔值,将这个布尔值赋值给全选按钮即可可达到单选按钮和全选按钮同步的效果
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯狂平头哥前端乐园

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值