JQ实现表格复选框一列全选

JQ实现表格复选框一列全选

JQ实现表格复选框一列全选

$(document).ready(function(){
        var $thr = $('table thead tr');
        var $checkAllTh = $('<th><input class="input" type="checkbox" id="checkAll" name="checkAll" lay-skin="primary"/></th>');
        /*将全选/反选复选框添加到表头最前,即增加一列*/
        $thr.prepend($checkAllTh);
        /*“全选/反选”复选框*/
        var $checkAll = $thr.find('input');
        $checkAll.click(function (event) {
            /*将所有行的选中状态设成全选框的选中状态*/
            $tbr.find('input').prop('checked', $(this).prop('checked'));
            /*并调整所有选中行的CSS样式*/
            if ($(this).prop('checked')) {
                $tbr.find('input').parent().parent().addClass('warning');
            } else {
                 $tbr.find('input').parent().parent().removeClass('warning');
            }
            /*阻止向上冒泡,以防再次触发点击操作*/
            event.stopPropagation();
        });
        /*点击全选框所在单元格时也触发全选框的点击操作*/
        $checkAllTh.click(function () {
            $(this).find('input').click();
        });
        var $tbr = $('table tbody tr');
        /*每一行都在最前面插入一个选中复选框的单元格*/
         var $checkItemTd = $('<td><input type="checkbox" name="checkItem" value="{$vo.id}" lay-skin="primary"/></td>');
         $tbr.prepend($checkItemTd);
        /*点击每一行的选中复选框时*/
        $tbr.find('input').click(function (event) {
            /*调整选中行的CSS样式*/
             $(this).parent().parent().toggleClass('warning');
            /*如果已经被选中行的行数等于表格的数据行数,将全选框设为选中状态,否则设为未选中状态*/
            $checkAll.prop('checked', $tbr.find('input:checked').length == $tbr.length ? true : false);
            /*阻止向上冒泡,以防再次触发点击操作*/
            event.stopPropagation();
        });
        /*点击每一行时也触发该行的选中操作*/

    })

   //复选框样式
    input[type='checkbox']{
          width: 20px;
          height: 20px;
          background-color: #fff;
          -webkit-appearance:none;
          border: 1px solid #c9c9c9;
          border-radius: 2px;
          outline: none;
          cursor:pointer;
      }
      input[type=checkbox]:checked{
          background: url("/static/images/right.png")no-repeat center;
          background-size: contain;
      }

复选框样式图复选框样式图片

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Emma'

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

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

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

打赏作者

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

抵扣说明:

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

余额充值