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

Thu*_*3eR 1 javascript jquery asp.net-mvc-5

设想

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

问题

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

代码

$(document).on("click", ".dialogTankClose", function() {
    //some code
})
Run Code Online (Sandbox Code Playgroud)

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

$(document).on("click", "modalCloseEvent",function(){
// how to catch this?
})
Run Code Online (Sandbox Code Playgroud)

我怎样才能抓住这个?

Ror*_*san 5

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

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

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

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

Bootstrap 文档中的更多信息