[英]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? 有没有更好的方法呢?
You should add onclick event listener on these inputs and "return false;" 您应该在这些输入上添加onclick事件侦听器并“return false;” Something like 就像是
$('a.className="popup"').onclick = function(){return false};
or 要么
<a href="#" onclick="return false;">Text</a> <a href="#" onclick="return false;">文字</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.