2017-04-12 122 views
0

我有一個表單,它在-some-元素集中時發生變化。
我想在離開頁面時發出警告。所以我保留一面旗幟,告訴我何時這些元素髮生了變化。
問題是該警示「這個頁面,要求您確認要離開」 - 總是提示;爲什麼beforeunload總是被觸發?

function checkSave(){ 
    return PageWasChanged; // it is correct, checked 
} 
$(window).on("beforeunload", function (eve) { 
    if (checkSave()) { 
     return true; 
    } 
    else { 
     return false; 
    } 
}); 
+1

您的代碼沒有提示。 – evolutionxbox

+0

There *具有*作爲dupetarget ... –

+0

@evolutionxbox:提示來自瀏覽器。 –

回答

1

beforeunload並不像其他事件及其處理程序應該返回undefined(唐。 「T顯示提示)或字符串(用於將用作提示的文字,而不是在現代瀏覽器)

這樣:

$(window).on.("beforeunload", function() { 
    if (!checkSave()) { 
     return "You have unsaved changes."; 
    } 
}); 

或沒有jQuery的:

window.onbeforeunload = function() { 
    if (!checkSave()) { 
     return "You have unsaved changes."; 
    } 
}; 
相關問題