使用bootbox不会触发show事件

all*_*sef 13 jquery twitter-bootstrap bootbox

我无法捕获bootbox.confirm对话框的show事件,以便在显示之前对对话框进行一些css调整.

我试过以下的事情,但没有成功:

$(document).on("show.bs.modal", function (event) {...});
$(document).on("show", function (event) {...});
$(document).on("show", ".bootbox", function (event) {...});
Run Code Online (Sandbox Code Playgroud)

cod*_*bia 41

这应该适用于您使用Bootbox 4.xx和Bootstrap 3.xx:

var box = bootbox.dialog({
  message: '',
  title: '',
  buttons: {},
  show: false
});

box.on("shown.bs.modal", function() {
  alert('it worked!');
});

box.modal('show');
Run Code Online (Sandbox Code Playgroud)

或者像这样:

$(document).on("shown.bs.modal", function (event) {...});
Run Code Online (Sandbox Code Playgroud)

  • 我发现当你没有在对话框上设置'show'选项或将其设置为true时,show.bs.modal事件不起作用.然后会自动显示该对话框,因此不会触发偶数.如果将show选项设置为false(如本例所示),并使用`box.modal('show')`手动显示模态,则也会触发show.bs.modal事件. (3认同)
  • 我不确定为什么选择当前的"正确"答案.****shows.bs.modal**对我有用 - 而**show.bs.modal**没有 (2认同)

all*_*sef 3

我还更喜欢使用全局函数,例如:

function bootbox_confirm(msg, callback_success, callback_cancel) {
    var d = bootbox.confirm({message:msg, show:false, callback:function(result) {
        if (result)
            callback_success();
        else if(typeof(callback_cancel) == 'function')
            callback_cancel();
    }});

    d.on("show.bs.modal", function() {
        //css afjustment for confirm dialogs
        alert("before show");
    });
    return d;
}
Run Code Online (Sandbox Code Playgroud)

并这样称呼它:

bootbox_confirm("my message", function(){alert('ok')}, function(){alert('cancel')}).modal('show');
Run Code Online (Sandbox Code Playgroud)

或者当没有取消逻辑时:

bootbox_confirm("my message", function(){alert('ok')}).modal('show');
Run Code Online (Sandbox Code Playgroud)