簡體   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