簡體   English   中英

window.onbeforeunload允許提交按鈕?

[英]window.onbeforeunload allow submit button?

當用戶離開頁面時,我使用以下JavaScript來顯示確認框。

我的問題是,即使用戶單擊我頁面中表單內的提交按鈕,它也會顯示。 我不希望在表單提交和跨度單擊時觸發此操作。

如何允許表單提交和跨度單擊下面的功能?

window.onbeforeunload = function (e) {
    e = e || window.event;



    return '';
};

編輯 -------------------------------

抱歉,它現在是一種表單,它是簡單的按鈕和href:我有一個頁面僅使用一個按鈕:

<input type="button" value="Save" id="btn-crop" />

和鏈接:

<a href="done.php" class=button2>Save</a>

您應該能夠使用e.currentTarget屬性檢查哪個對象調度了事件,然后在必要時顯示提示。

您可以為頁面中的每個按鈕在提交事件的回調中調用[Event].stopPropagation

該函數將在提交按鈕時停止beforeunload事件的回調函數。

您可以在此處閱讀有關[Event].stopPropagation更多信息。

像這樣: Button.addEventListener("submit",function(Event){Event.stopPropagation()})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM