2011-06-08 88 views
0

我有下面的代碼來處理未保存的表單數據的情況下,以提示用戶但這種代碼也顯示在點擊時發出警報提交按鈕window.onbeforeunload警報提交

$(document).ready(function() { 
    var _isDirty = false; 
    $(":input").live("change", function() { 
     _isDirty = true; 
    }); 

    $(':[type=submit]').live('click', function() { 
     _isDirty = false;    
    }); 
    window.onbeforeunload = function() { 
     if (_isDirty) {    
      return 'You have made changes to data on this page. If you navigate away from this page without first saving your data, the changes will be lost.'; 
     } 
     else {   
      return null; 
     } 
    } 
} 
+0

獲取事件並使用preventDefault – 2011-06-08 10:30:44

+0

您可以檢查代碼_isDirty = false是否實際執行? – Kangkan 2011-06-08 10:31:45

回答

0

關注這post爲您的答案。

2

onbeforeunload有一個怪癖,如果你返回任何東西,甚至空,它會拋出警報。嘗試刪除其他子句else {return null;},它應該停止顯示。