如何检查脏标志 [英] How to check for dirty flag
问题描述
我想知道如果用户选择从中导航页面,检查页面是否脏的最佳方法是什么。例如,有一个注册表格,用户输入他的所有信息。然后不小心点击链接从中导航。
I was wondering what is the best way of checking if a page is dirty if a user chooses to navigate it from it. For example, there is a registration form and the user enters all his information. Then accidentally clicks on a link to navigate from it.
我在网上发现了这一点,如果一个人对任何表单输入值进行更改,它会检查页面是否脏。
I found this on the web where it checks if a page is dirty if a person makes changes to any of the form input values.
<script type="text/javascript">
var isDirty = false;
var msg = 'You haven\'t saved your changes.';
$(document).ready(function(){
$(':input').change(function(){
if(!isDirty){
isDirty = true;
}
});
window.onbeforeunload = function(){
if(isDirty){
return msg;
}
};
});
</script>
所以这很好用。但是如何排除某些弹出窗口链接?有没有更好的方法呢?
So this works great. But how do I exclude some links that are pop-ups? Is there a better way of doing this?
推荐答案
你应该在这些输入上添加onclick事件监听器并return false;
类似
You should add onclick event listener on these inputs and "return false;" Something like
$('a.className="popup"').onclick = function(){return false};
或
< a href = #onclick =return false;>文字< / a>
<a href="#" onclick="return false;">Text</a>
这篇关于如何检查脏标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!