将标准Javascript确认对话框转换为Bootbox确认

时间:2014-03-06 21:53:56

标签: javascript asp.net-mvc bootbox

  $(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");
                    }
                });
        });

1 个答案:

答案 0 :(得分:0)

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

Bootbox对话框允许您将回调函数附加到按钮,因此您可以为每个按钮指定要执行的特定功能。

我还添加了行closeButton: false,,以便用户无法点击关闭按钮来关闭对话框,而是必须点击OkCancel

 $(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;
    }
});