0

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

事件处理程序以下列方式附加:

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

关于如何确保此处理程序中的代码仅在 modal show 上触发的任何想法?

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

4

1 回答 1

0

有一种解决方法 - 检查 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
});

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

于 2018-06-22T21:34:24.117 回答