【问题标题】:Using bootbox the show event is not fired使用 bootbox 不会触发 show 事件
【发布时间】:2014-04-21 13:30:28
【问题描述】:

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

我尝试了以下方法但没有成功:

$(document).on("show.bs.modal", function (event) {...});
$(document).on("show", function (event) {...});
$(document).on("show", ".bootbox", function (event) {...});

【问题讨论】:

    标签: jquery twitter-bootstrap bootbox


    【解决方案1】:

    这应该适合您使用 Bootbox 4.x.x 和 Bootstrap 3.x.x:

    var box = bootbox.dialog({
      message: '',
      title: '',
      buttons: {},
      show: false
    });
    
    box.on("shown.bs.modal", function() {
      alert('it worked!');
    });
    
    box.modal('show');
    

    或者像这样:

    $(document).on("shown.bs.modal", function (event) {...});
    

    【讨论】:

    • 我不确定为什么选择当前的“正确”答案。这个 shown.bs.modal 对我有用 - 而 show.bs.modal 没有
    • 我发现show.bs.modal 事件在你没有在对话框上设置'show' 选项时不起作用,或者将它设置为true。然后会自动显示对话框,因此不会触发事件。如果将 show 选项设置为 false,如本例所示,并使用 box.modal('show') 手动显示模式,那么也会触发 show.bs.modal 事件。
    • 这对我有用:bootbox.dialog({ message: '', title: '', buttons: {}, show: false }).one("shown.bs.modal", function () { alert('成功了!'); });
    • 这绝对解决了问题,应该被接受为答案
    【解决方案2】:

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

    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;
    }
    

    这样称呼它:

    bootbox_confirm("my message", function(){alert('ok')}, function(){alert('cancel')}).modal('show');
    

    或者当没有取消逻辑时:

    bootbox_confirm("my message", function(){alert('ok')}).modal('show');
    

    【讨论】:

    • 我希望人们能够评论为什么他们认为答案不好,而不是仅仅投反对票,特别是如果答案被接受(意味着它对提问者有效)。
    • @AustinSchmidt 我认为这是因为他从未指定答案需要在全局函数中。他选择了有效的答案,然后将其应用于自己的答案,然后将其标记为正确。我个人并不真正关心它(我写了最高赞成的答案),但我可以看到其他人可能会对此提出异议。
    【解决方案3】:

    你可以这样做:

    var dialog = bootbox.dialog("foo", [], {show: false});
    
    dialog.on("shown", function() {
     //
    });
    
    dialog.modal("show");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 2014-09-18
      • 1970-01-01
      • 2015-06-25
      • 2019-01-14
      • 1970-01-01
      • 2014-11-30
      相关资源
      最近更新 更多