2013-03-13 127 views
0

我正在使用此jquery代碼來警告用戶在完成我們的在線應用程序之前不要導航。當他們在菜單/列表字段中時,我們有很多用戶點擊後退按鈕(不知道它是導航捷徑),並丟失了他們在應用程序中輸入的所有數據。問題是,我無法弄清楚如何在他們點擊提交按鈕時不顯示警告。防止提交時顯示jquery警報

我已經看過這個網站上的其他幾個類似的帖子,但我無法讓他們工作。其實對於大多數人來說,我甚至無法讓彈出窗口工作。我對jquery不是很熟悉,所以我可能很簡單。 (其實,我有另一種解決方案,我正在這裏:http://www.citizensmemorial.com/careers/online-app.html

<script> function setConfirmUnload(on) { 

    window.onbeforeunload = (on) ? unloadMessage : null; 

} 

function unloadMessage() { 

    return 'You have started filling out this application.' + 
     ' If you navigate away from this page without' +   
     ' first saving your data, the changes will be' + 
     ' lost.'; 

}</script> 
<script> 
$(document).ready(function() { 

    $(':input',document.form1).bind(
     "change", function() { 
       setConfirmUnload(true); 
     } 
    ); // Prevent accidental navigation away 

}); 
</script> 

回答

0

所以你沒有設置確認時解析什麼setConfirmUnload我已經扭轉你的驗證

function setConfirmUnload(disable) { 
    window.onbeforeunload = disable ? null : unloadMessage; 
} 

function unloadMessage() { 
    return 'You have started filling out this application.' + 
     ' If you navigate away from this page without' +   
     ' first saving your data, the changes will be' + 
     ' lost.'; 
} 
setConfirmUnload(); 
jQuery(function($) { // <-- Dom is ready (same as $(document).ready(function($) {) 
    $("form").on("submit", setConfirmUnload); 
}); 

,我有。約束setConfirmUnload的形式提交處理程序中調用的第一個參數是一個jQueryEvent Object測試時,這將驗證爲真。

You can try a simple demo here


如果你想先激活卸載處理程序時,將輸入到輸入任何你可以使用:

var $inputs = $("form :input").on("change", function() { 
    setConfirmUnload(); 
    $inputs.off("change"); // <-- remove the event form all elements. 
}); 
+0

我想在我們的內容管理系統使用此代碼(有一個地方爲模板輸入代碼),我得到這個錯誤:在編輯過程中發生錯誤:元素的內容必須包含格式良好的字符數據或標記。 – user2087788 2013-03-14 18:24:57