列表点击编辑的时候多选删除

46 篇文章 0 订阅
41 篇文章 1 订阅

在做收藏和消息列表点击编辑进行全选删除和单选删除时,遇到了一个这样的问题:当点击编辑了之后,后面加载出来的列表条目不会渲染上去多选框,也添加不上被选中的状态,这样就导致无法判断是否被选中。
请教了一番后,才知道需要把后加载出来的列表先解绑再绑定checked的状态。
全选按钮:

<input id="all" type="checkbox" class="dr_select" data="all">

列表条目的多选按钮:

<input type="checkbox" class="dr_select" value="这里可以写id用来做删除的时候传参">

js:

$(document).ready(function () {
    $('.dr_select').unbind('click').bind('click', '', checkBox);
});
function checkBox (){
    if ($(this).attr("checked")) {
        $(this).removeAttr("checked");
        return;
    }
    $(this).attr("checked", "ture");
}
//最后一个checkbox 全选或者全反选
$('#all').on('click', function() {
    if ($(this).attr("index")) {
        $('.dr_select').removeAttr('checked').removeProp('checked');
        $(this).removeAttr("index");
        return;
    }
    $(this).attr("index", 1);
    $('.dr_select').attr('checked', 'ture').prop('checked', 'ture');
})

ps:因为我的这个有一个加载列表的操作,所以我做了加载完列表的时候判断了一下,首先给编辑按钮edit赋了一个值,比如1,当点击edit的时候,编辑的字肯定就变成完成了把,这时候编辑按钮的1就变成了2。然后在加载完列表的时候判断编辑按钮的值是2的话,就把点击编辑时会改变的东西再执行一遍,也要执行一下一开始我们的解绑操作(下面这句话),这样弄完之后,就好使了。

$('.dr_select').unbind('click').bind('click', '', checkBox);

最后是删除操作的代码。这个地方是传的上面input多选框里给value赋的值,如果是多选的话用逗号隔开传进去。这里我引了layer,不需要的话可以删掉:

//点击删除
$(".collect_delete").on('click', function () {
    // 获取url值
    var url = $(this).attr("url");
    let id = $(".dr_select").val();
    console.log(id)
    // 询问弹窗
    layer.open({
        title: [
            Confirm_delete,
            'background-color:#ffffff !important;color: #000 !important;height: 2.11rem;line-height: 2.11rem;font-size:0.3rem;'
        ],
        type: 1,
        anim: 'up',
        btn: [yes, cancel],
        yes: function () {
            // yes
            let value = $(".dr_select[checked]");
            var arr = [];
            for (var i = 0; i < value.length; i++) {
                arr.push(value[i].getAttribute("value"));
            }
            if (arr) {
                var id = arr.join(',');
            }
            $.ajax({
                type: "POST",
                url: url,
                data: {
                    id: id,
                },
                dataType: "json",
                success: function (data) {
                    if (data.status == 1) {
                        // 操作成功
                        layer.open({
                            content: '删除成功',
                            skin: 'del_success',
                            time: 2
                        });
                        window.location.reload();
                    } else {
                        layer.open({
                            title: ['请正确勾选'],
                            type: 1,
                            anim: 'up',
                        });
                    }
                }
            });
        }, function() {
            layer.closeAll();
        }
    })
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值