【问题标题】:Remove button after select jquery confirm button选择jquery确认按钮后删除按钮
【发布时间】:2018-11-27 03:52:01
【问题描述】:

如何在选择 jquery 'Confirm' 按钮后删除特定行的 'Revoke' 按钮?下面的代码不会永久删除“撤消”按钮。

<td>
    <div class="btn btn-primary revoke-btn">Revoke</div>
</td>

$(document).ready(function() {
    $('.revoke-btn').click(function(e){
        e.preventDefault();
        $.confirm({
            title:'Confirm?',
            content:'Confirm to revoke?',
            buttons:{
                confirm: {
                    text: 'Confirm',
                    btnClass: 'btn-danger',
                    action: function () {
                        $(this).remove();
                        var link = $(e.currentTarget).parent().attr('href');
                        window.location.href=link;
                    }
                },
                cancel: {
                    text: 'Cancel',
                    btnClass: 'btn-default'
                }
            }
        })
    });
});

【问题讨论】:

    标签: jquery jquery-confirm


    【解决方案1】:

    在$.confirm中,不能获取clicked元素,所以需要获取 变量中的元素,然后您可以将此变量用于 $.confirm 动作函数。

    $(document).ready(function() {
        var isBtn = localStorage.getItem("button");
    
        if(isBtn == "Removed")
        {
              $('.revoke-btn').remove()
        }
    
        $('.revoke-btn').click(function(e){
            e.preventDefault();
            var elem = jQuery(this);
            $.confirm({
                title:'Confirm?',
                content:'Confirm to revoke?',
                buttons:{
                    confirm: {
                        text: 'Confirm',
                        btnClass: 'btn-danger',
                        action: function () {
                            localStorage.setItem("button", "Removed");
                            elem.remove();
                            var link = $(e.currentTarget).parent().attr('href');
                            window.location.href=link;
                        }
                    },
                    cancel: {
                        text: 'Cancel',
                        btnClass: 'btn-default'
                    }
                }
            })
        });
    });
    

    【讨论】:

    • 一些解释将使这成为 OP 的有用答案。
    • 但是刷新页面后,按钮还在
    • 所以我认为您需要使用浏览器本地存储来实现此功能..
    • 有这方面的例子吗?
    【解决方案2】:
     $('.revoke-btn').click(function (e) {
                e.preventDefault();
                var btn = $(this);
                $.confirm({
                    title: 'Confirm?',
                    content: 'Confirm to revoke?',
                    buttons: {
                        confirm: {
                            text: 'Confirm',
                            btnClass: 'btn-danger',
                            action: function () {
                                console.log(btn);
                                console.log(this);
                                btn.remove();
                                var link = $(e.currentTarget).parent().attr('href');
                                window.location.href = link;
                            }
                        },
                        cancel: {
                            text: 'Cancel',
                            btnClass: 'btn-default'
                        }
                    }
                })
            });
    

    在操作功能 $(this) 将不适用于您单击的按钮。只需在控制台上打印以上输出即可获得它

    【讨论】:

    • 刷新页面后,按钮还在
    • 您必须使用本地存储,但这只能在一台电脑或浏览器中使用。如果您在其他浏览器或电脑中打开此链接,按钮将再次出现
    猜你喜欢
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 2015-03-14
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多