【问题标题】:Replace JavaScript confirm alert with bootbox.js confirm将 JavaScript 确认警报替换为 bootbox.js 确认
【发布时间】:2014-05-20 07:39:14
【问题描述】:

我不太了解如何实施 bootbox.js 来替换我的标准 javascript 确认警报弹出窗口,需要一些帮助。我已经阅读了一些主题和文档,但不能完全正确。

我目前有这个按钮/链接在我的模板中保存记录号:

<a href="{% url overseas_experience_details_duplicate overseas_experience_detail.id %}" onclick="if(confirmDuplicate())showProgressAnimation();else return false;">{% trans "Duplicate" %}</a>

这会调用我想用 bootbox.js 替换的当前 js 函数:

function confirmDuplicate() {
    return confirm("Are you sure?");
}

如果选中 OK 选项,则复制记录(复制记录时会显示显示进度动画)。如果选择了 CANCEL 选项,则不会发生任何事情。

我已经使用以下代码来实现引导箱警报,但我不知道如何返回引导箱 OK 确认,然后将复制记录。

bootbox.js

function confirmDuplicate() {
    bootbox.confirm("Are you sure?", function(result) {
    if (result == true) {

        // not too sure what to insert here to return the bootbox confirm();

    }
});

我们将不胜感激一些有效的建议

问题更新

我现在有 bootbox 确认弹出显示,但我无法返回“true”或“false”确认结果。这是我的新代码:

function confirmDuplicate() {
    bootbox.confirm({
        callback: function(result) {
            if (result) {
                return result;
            }
        }
    }
}

我很沮丧,因为我无法返回结果来复制记录。

任何帮助都会很棒。

【问题讨论】:

  • 为什么要在 html 中编写 javascript?我看到你在问题中有 jquery 作为标签,如果你真的使用它,它会变成一个非常简单的任务

标签: javascript jquery bootbox


【解决方案1】:

试试这样的:

<a href="whatever" onclick="return myConfirm(this.href)">....

myConfirm = function(url) {
    bootbox.confirm("sure?", function(okay) {
        if(okay)
             location.href = url;
    });
    return false;
}

您无法立即获得bootbox.confirm结果,因此从处理程序中返回false以防止链接被跟踪并在回调函数中执行所需的操作。

【讨论】:

  • 我已经尝试了你的例子,但仍然无法返回被点击的按钮!
  • @user3354539:这就是重点:你不能从回调中返回。你应该在回调中做一些事情,而不是返回。
  • 好的我现在可以看到我做错了什么,但是我仍然不知道如何在回调中编写js代码以触发超链接中的重复href,如上问题所述:{% trans "Duplicate" %}
【解决方案2】:

继续我对您的问题的评论,这里有一个不显眼的 jquery 版本:

$('.your-link-class').on('click', function(e) {
   e.preventDefault(); //this would be equivalent to the return false in your code
   var self = this;
   bootbox.confirm("Are you sure?", function(result) {
       if (result) {
           // Here you have access to the jquery object of your link with $(self)
           // and you can do stuff like, for example:
           var clone = $(self).clone();
           clone.insertAfter($(self));
       }
    });
});

还有一个演示(不是:在演示中我使用的是传统提示,因为我没有加载引导箱源):

【讨论】:

  • 感谢您的建议,但我仍然无法返回确认结果。
【解决方案3】:
function isConfirm(message, elem) {

    bootbox.confirm(message, function (result) {
        if (result)
            window.location.href = elem;
    });

    return false;
}
<a href='@Url.Action("DeleteAction", "Admin", new { id = Model.ActionObj.id })' onclick="javascript: return isConfrm('Are you sure you want to delete this action?', this)">  Delete </a>

将其用于功能:

function isConfirm(message, elem) {
    bootbox.confirm(message, function (result) {
        if (result)
            window.location.href = elem;
    });

    return false;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多