Bootbox:关闭对话框/单击 'X' 按钮后的回调函数

Bootbox: Callback function after dismissing the dialog / Clicking on the 'X' button

以下代码片段允许我在点击按钮的回调函数中执行操作。但是,如何获得回调函数或类似的解决方法,以便在用户单击 'X' button/dismisses 对话框时执行一些代码?

    bootbox.dialog({
        title: "Woah this acts like an alert",
        message: "Cool info for you. You MUST click Ok.",
        buttons: {
            sucess:{
                label: "Ok",
                callback: callback
            }
        }       
    });

   callback(){//stuff that happens when they click Ok.}

我不想 disable/hide 关闭按钮

closeButton: false,

您可以使用变量来检查在单击 OKx button / escape key

后模态框是否被隐藏
var status = false;

$('.btn').on('click', function () {
    bootbox.dialog({
        title: "Woah this acts like an alert",
        message: "Cool info for you. You MUST click Ok.",
        buttons: {
            sucess: {
                label: "Ok",
                callback: function () {
                    status = true;
                }
            }
        },
        onEscape: function () {
            $('.bootbox.modal').modal('hide');
        }
    });
});

$(document).on("hidden.bs.modal", ".bootbox.modal", function (e) {
    callback();
});


function callback() {
    if (!status) {
        onClose();
    } else {
        onOK();
        status = false;
    }
}

function onClose() {
    $('p.alert span').removeClass().addClass('text-danger').text("Dismissed");
}

function onOK() {
    $('p.alert span').removeClass().addClass('text-success').text("Sucess");
}

Fiddle demo

这个有onEscape函数。

bootbox.dialog({
    message: 'the msg',
    title: "Title",
    onEscape: function() {
        // you can do anything here you want when the user dismisses dialog
    }
}); 

有些人可能认为这是一种变通。虽然这很适合我,但我想确认作为开发人员,有人接受了消息,这触发了下一个事件。

使用 Bootbox.js 的本机 confirm() 方法, 提供 callback 操作。我添加了一个额外的 class 作为 confirm 按钮的选项( 必须 confirm() 调用中提供)和 hidden classname(例如,Bootstap 有一个用于 display:none 的助手 class,名为 hidden

这会隐藏确认按钮,因此模态框显示为普通的警告框。

    bootbox.confirm({ 
        message: "Some Button Text", 
        buttons: {
            "cancel": {
                label: "<i class='fa fa-check'></i> OK - I understand",
                className: "btn btn-primary"
            },
            //Hide the required confirm button.
            "confirm": { label: "", className: "hidden" }
        },
        callback: function(){
            //Begin Callback
            alert( "Finished" );
        }
    });

JsFiddle Example