简体   繁体   English

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

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

Scenario :场景

  1. I click on a button我点击一个按钮
  2. Ajax call is made to the server Ajax 调用服务器
  3. data is returned and modal is shown返回数据并显示模态

Problem :问题

When user clicks on the close button or the "X" in the corner I catch this event by assigning a class to these two elements and assigning an event to this class.当用户单击关闭按钮或角落的“X”时,我通过将 class 分配给这两个元素并将事件分配给该 class 来捕获此事件。

Code :代码

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

My problem is that i can't figure out how to catch when the user clicks outside of the dialog or presses "escape".我的问题是当用户在对话框外单击或按“转义”时,我无法弄清楚如何捕捉。

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

How can I catch this?我怎么能抓住这个?

The Bootstrap modal raises an event when it closes, which you can hook to: hidden.bs.modal . Bootstrap模态在关闭时会引发一个事件,您可以将其挂钩到: hidden.bs.modal This event fires no matter how the modal is closed. 无论如何关闭模式,都会触发此事件。 Try this: 尝试这个:

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

You can use a delegated event handler if the modal is dynamically added to the DOM: 如果将模式动态添加到DOM,则可以使用委托事件处理程序:

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

More information in the Bootstrap documentation Bootstrap文档中的更多信息

You can use 'hidden.bs.modal' modal method to run custom code while modal is getting unload / hide from document.您可以使用“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.

相关问题 当用户在模态外单击时关闭模态 - Close the modal when user clicks outside the modal 当用户在其外部单击时关闭模式,为什么有==而不是!=? - Close modal when user clicks outside of it, why is there == and not !=? 用户在菜单外单击时如何关闭Bootstrap导航栏下拉菜单? - How to close the Bootstrap navbar dropdown when the user clicks outside the menu? 当用户在 Google map 之外点击时如何触发事件 - How to fire event when user clicks outside Google map Angular 5-当用户在目标元素之外单击时如何触发事件 - Angular 5 - How to trigger event when user clicks outside targeted elements 如果在模态对话框外单击,则如何禁用用户所做的所有单击 - how to disable all the clicks made by the user if click is made outside the modal dialog 当用户在JQuery UI中的模式对话框之外单击时,如何防止模式对话框关闭? - How do you prevent Modal dialog from closing when users clicks outside of the modal dialog box In JQuery UI? 用户何时在div之外单击时的jQuery事件? - JQuery Event for when user clicks outside a div? 当用户点击模态窗口之外时,如何使用jquery关闭w3schools示例中的模态窗口? - How to use jquery to close the modal window in the w3schools example when user clicks outside of modal window? 如何禁用jQuery模式对话框外的所有点击? - How to disable all clicks outside jquery modal dialog?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM