【问题标题】:Cannot listen to bootbox hidden.bs.modal event无法收听 bootbox hidden.bs.modal 事件
【发布时间】:2017-05-02 06:35:24
【问题描述】:

我必须使用引导标准事件来监听所有模态隐藏事件:

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

我还需要将此事件用于引导框对话框,但这不起作用。查看我找到的引导箱源代码(https://github.com/makeusabrew/bootbox/blob/master/bootbox.js 的第 659 行):

 dialog.one("hidden.bs.modal", function(e) {
      // ensure we don't accidentally intercept hidden events triggered
      // by children of the current dialog. We shouldn't anymore now BS
      // namespaces its events; but still worth doing
      if (e.target === this) {
        dialog.remove();
      }
});

如果这个 bootbox 回调在我的回调之前被调用,我就绝望了,因为这会破坏调用对象。

是否可以绕过这个问题?如何调用所有模式隐藏事件(也适用于引导框模式)?

【问题讨论】:

    标签: twitter-bootstrap dom-events bootbox


    【解决方案1】:

    您可以在 5.4 版中使用“onHidden”。

    bootbox.alert({
            message: 'You message',
            onHidden: function(e) {
                /* e is the hide.bs.modal event */
                if($(".modal").hasClass('show')){
                    $('body').addClass('modal-open');
                }
            } 
        });
    

    【讨论】:

      【解决方案2】:

      假设 ... 代码可以被封装,您可以为您进行的每个 Bootbox 调用订阅事件。比如:

      function doSomethingWhenModalCloses(){
          ...
      }
      
      bootbox
          .alert('I am an alert!')
          .on('hidden.bs.modal', doSomethingWhenModalCloses); // repeat for other functions
      

      这显然不是最优的,但 Bootbox 并没有真正提供以全局方式委派这些事件的内置方法。

      【讨论】:

        【解决方案3】:

        所以我知道这是一篇旧帖子,但没有人回答。这是我的工作解决方案

         dialog.on('hidden.bs.modal', function () {
            console.log('SHOW ME WHEN HIDDEN!')
         })
        

        为了在上下文中显示它,我做了一个jsFiddle

        【讨论】:

          猜你喜欢
          • 2022-01-04
          • 1970-01-01
          • 2020-05-19
          • 1970-01-01
          • 2013-10-10
          • 1970-01-01
          • 2021-07-07
          • 2018-12-28
          • 2016-04-25
          相关资源
          最近更新 更多