【问题标题】:Turning a Standard Javascript Confirm Dialog into Bootbox Confrim将标准 Javascript 确认对话框转换为 Bootbox 确认
【发布时间】:2014-03-06 21:53:56
【问题描述】:
  $(document).ready(function () {
            $("#close").click(function () {
                var link = $(this).attr("href"); // "get" the intended link in a var
                var result = confirm("Are you sure?");
                if (result) {
                    document.location.href = link;  // if result, "set" the document location      
                }
            });
        });

如何使用 Bootbox 对话框而不是 JavaScript 对话框?

编辑

好吧,我已经尝试过了,但是在单击启动框对话框中的“确定”按钮时没有任何反应

        $(document).on("click", "#close", function (e) {
            e.preventDefault();
            var link = $(this).attr("href"); // "get" the intended link in a var
            bootbox.confirm("Are you sure you want to close this Incident? This operation cannot be reversed.", function (result) {
                    if (result) {
                        document.location.href = link;
                    } else {
                        console.log("user declined");
                    }
                });
        });

【问题讨论】:

  • 我在我的 JS 调试器中遇到一个错误,那就是:不推荐使用 event.returnValue。请改用标准 event.preventDefault()。即使我已经在使用 preventDefault :s
  • 这不是错误。而且它不是由您的代码引起的。
  • 在 bootbox.confirm 之前放置一个 console.log,是否到达?
  • 好的。它确实达到了是的
  • 您确定您使用的引导箱正确吗?我以前从未使用过它,但这是此调试过程中的下一个合乎逻辑的步骤。

标签: javascript asp.net-mvc bootbox


【解决方案1】:

我会考虑使用 Bootbox 对话框,如下所示。

引导框对话框允许您将回调函数附加到按钮,因此您可以为每个按钮指定要发生的特定函数。

我还添加了closeButton: false, 行,这样用户就不能单击关闭按钮来关闭对话框,而是必须单击Ok 或Cancel。

 $(document).on("click", "#close", function (e) {
    e.preventDefault();
    var link = $(this).attr("href"); // "get" the intended link in a var

    bootbox.dialog({
        closeButton: false,
        message: "Are you sure you want to close this Incident? This operation cannot be reversed.",
        buttons: {
            cancel:{
                label: "Cancel",
                callback: doThisOnCancel
            },
            ok:{
                label: "Ok",
                callback: doThisOnOk
            }
        }       
    });

    doThisOnCancel(){
        console.log("user declined");               
    }

    doThisOnOk(){
        document.location.href = link;
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多