繁体   English   中英

当用户在引导模式对话框之外单击时如何捕获事件?

[英]How to catch event when user clicks outside of bootstrap modal dialog?

场景

  1. 我点击一个按钮
  2. Ajax 调用服务器
  3. 返回数据并显示模态

问题

当用户单击关闭按钮或角落的“X”时,我通过将 class 分配给这两个元素并将事件分配给该 class 来捕获此事件。

代码

$(document).on("click", ".dialogTankClose", function() {
    //some code
})

我的问题是当用户在对话框外单击或按“转义”时,我无法弄清楚如何捕捉。

$(document).on("click", "modalCloseEvent",function(){
// how to catch this?
})

我怎么能抓住这个?

Bootstrap模态在关闭时会引发一个事件,您可以将其挂钩到: hidden.bs.modal 无论如何关闭模式,都会触发此事件。 尝试这个:

$('#bootstrapModal').on("hidden.bs.modal", function() {
    $.automation.worker.bindIntervalEvent("#TanksContent", "/Tank/GetTanks", function () {
        $.automation.tanks.tableInit();
    });
});

如果将模式动态添加到DOM,则可以使用委托事件处理程序:

$(document).on("hidden.bs.modal", '#bootstrapModal', function() {
    $.automation.worker.bindIntervalEvent("#TanksContent", "/Tank/GetTanks", function () {
        $.automation.tanks.tableInit();
    });
});

Bootstrap文档中的更多信息

您可以使用“hidden.bs.modal”模态方法在模态从文档中卸载/隐藏时运行自定义代码。

$('#your-modal-ID').on('hidden.bs.modal', function (e) {
  console.log("Hey !! I am unloading... ");
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM