【问题标题】:Event show.bs.modal fired on .show() for inside-modal elements事件 show.bs.modal 在 .show() 上为内部模态元素触发
【发布时间】:2018-12-02 09:34:49
【问题描述】:

我刚刚意识到,事件 show.bs.modal 不仅会在显示模态本身时触发,而且还会在每次为模态内的元素调用 .show() 方法时触发。

事件处理程序的附加方式如下:

$('#modalName').on('show.bs.modal', function(event) { ... });

关于如何确保此处理程序中的代码仅在模态显示时触发的任何想法?

尤其是当您在模式中使用 formValidator.io 时会出现问题(一旦表单字段验证失败,它会为元素调用 .show())

【问题讨论】:

    标签: javascript event-handling bootstrap-4 bootstrap-modal formvalidation.io


    【解决方案1】:

    有一种解决方法 - 检查 event.relatedTarget 调用的 .show() 并在无意中调用它时简单地停止代码运行 - 例如:

    $('#modalName').on('show.bs.modal', function(event) {    
        if (!$(event.relatedTarget).parents(this).length) // checks if event has been executed by running .show() from outside the modal
            return false; // this will terminate and rest of the code will not be executed
        ... // other things you need to do on the event handler
    });
    

    但这只是一种解决方法 - 任何其他建议,这将使该解决方案更加优雅,非常受欢迎。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      • 2019-09-25
      • 2023-03-08
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多