Bootbox:解除对话框后的回调函数/单击"X"按钮

Nul*_*nce 9 javascript callback twitter-bootstrap bootbox

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

    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.}
Run Code Online (Sandbox Code Playgroud)

我不想禁用/隐藏关闭按钮

closeButton: false,
Run Code Online (Sandbox Code Playgroud)

Har*_*man 13

这有onEscape功能.

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

  • 只有在点击"esc"键时,单击X时才会触发此操作.anpsmn的答案适用于两者. (4认同)

anp*_*smn 6

您可以使用变量来检查单击OK或后是否隐藏了模态x 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");
}
Run Code Online (Sandbox Code Playgroud)

小提琴演示