【问题标题】:JQuery - Removing a row of content using a plugin to confirmJQuery - 使用插件删除一行内容以确认
【发布时间】:2014-04-29 09:27:07
【问题描述】:

我有这个标记:

<div class="form-group conteiner">
    <div class="row">
        <div class="col-md-2">
            <label for="date">Date:</label>
            <div class="input-group date datetimepickaa"  id="datetimepickerloop1" data-date-format="YYYY/MM/DD">
            <input type="text" class="datetimepickaa"  data-date-format="YYYY/MM/DD"  class="datetimepickaa" />
            <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
            </div>
        </div>
        <div class="col-md-9">
            <label for="notes">Notes:</label>
            <textarea class="form-control autosize" id="" name="">Lorem ipsum dolor sit amet.</textarea>
        </div>
        <div class="col-md-1">
        <button type="button" class="removee" >Delete</button>
        </div>
    </div>
</div>

当您单击“删除”按钮时,我正在使用此 JQuery 代码删除一行内容:

$("button.removee").click(function(){
    $(this).closest(".conteiner").remove();
});

我想使用这个 JQuery 来请求确认: http://myclabs.github.io/jquery.confirm/

在文档中,它说我们需要像这样使用插件:

$(".confirm").confirm({
    text: "Are you sure you want to delete that comment?",
    title: "Confirmation required",
    confirm: function(button) {
        // do something
    },
    cancel: function(button) {
        // do something
    },
    confirmButton: "Yes I am",
    cancelButton: "No",
    post: false
});

我正在尝试使用与我的代码混合的插件进行删除:

$("button.removee").confirm({
    text: "Are you sure you want to delete this row?",
    title: "Confirmation required",
    confirm: function() {
        $(this).closest(".conteiner").remove();
    },
    cancel: function() {
        // do something
    },
    confirmButton: "Yes I am",
    cancelButton: "No",
    post: false
    });

在这一行中,(this) 不再指代 $("button.removee"):

$(this).closest(".conteiner").remove();

所以,它不工作。你对如何做到这一点有任何线索吗?

先谢谢了!!

【问题讨论】:

    标签: javascript jquery jquery-plugins scope confirm


    【解决方案1】:

    未经测试,但这可能会奏效。在触发确认框之前保存每个按钮的按钮实例。

    $(document).on("click", "button.removee", function()
    {
        var btn = $(this);
    
        $(this).confirm({
            text: "Are you sure you want to delete this row?",
            title: "Confirmation required",
            confirm: function() {
                btn.closest(".conteiner").remove();
            },
            cancel: function() {
                // do something
            },
            confirmButton: "Yes I am",
            cancelButton: "No",
            post: false
        });
    });
    

    编辑:更新答案以反映将事件绑定到动态添加的按钮所需的更改。

    【讨论】:

    • 像这样它也可以工作......但在我的示例中不是,因为我需要在 ADD 元素功能中使用代码(我正在实现“添加/删除输入字段”解决方案)。所以每当我添加一个新元素时,它都会将确认对话框绑定到每一行。这意味着我必须多次确认才能删除一个元素。
    • 我已经更新了代码。它现在应该也可以与动态添加的按钮一起使用。只需确保只调用一次代码(在document.ready() 中的页面初始化期间或其他什么)。
    【解决方案2】:

    尝试使用此代码

    $("button.removee").confirm({
    text: "Are you sure you want to delete this row?",
    title: "Confirmation required",
    confirm: function() {
        $("button.removee").closest(".conteiner").remove();
    },
    cancel: function() {
        // do something
    },
    confirmButton: "Yes I am",
    cancelButton: "No",
    post: false
    });
    

    【讨论】:

    • 此代码仅在您有一行内容时才有效,但我有不止一个。因此,它会删除所有行,而不是您单击“删除”按钮的那一行...还是谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    相关资源
    最近更新 更多