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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29113222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:52:26  来源:igfitidea点击:

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

javascripttwitter-bootstrapcallbackbootbox

提问by Null Reference

The following snippet allows me to perform stuff in a callback function for the buttons that are clicked. However, how can I get a callback function, or a similar workaround such that I can perform some code when a user clicks on the 'X' button/dismisses the dialog?

以下代码段允许我在单击的按钮的回调函数中执行操作。但是,如何获得回调函数或类似的解决方法,以便在用户单击“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.}

I do not want to disable/hide the close button with

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

closeButton: false,

回答by Haris ur Rehman

There is onEscape function for this.

为此有 onEscape 功能。

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

回答by anpsmn

You can use a variable to check if the modal was hidden after a click on OKor x button / escape key

您可以使用变量来检查单击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

小提琴演示

回答by MackieeE

Some people might see this as a bit of a hack-around. Although it suits me fine as all I wanted to acknowledgeas a developer that someone accepted the message, which triggered the next event.

有些人可能会认为这是一种黑客攻击。虽然它很适合我,因为所有我想承认作为一个开发者,有人接受了消息,从而引发了下一个事件。

Using Bootbox.js' native confirm()method which doessupply a callbackaction. I added an additional class as an option to the confirmbutton (which mustbe supplied on a confirm()call) with the hiddenclassname (E.g. Bootstap has a helper class for display:nonecalled hidden.

使用Bootbox.js“本地confirm()哪种方法供应callback的行动。我添加了一个额外的类作为confirm按钮的选项(必须confirm()调用时提供)和hidden类名(例如 Bootstap 有一个用于display:none调用的辅助类hidden

This hides the confirm button, thus the Modal appears as a normal Alert box.

这隐藏了确认按钮,因此模态显示为正常的警报框。

    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

JsFiddle 示例