[英]Dialog action before leaving a page
我找到了很多解决方案,但没有一个能真正解决我的问题。 如果用户决定不保存更改就离开,我必须在页面上执行某些操作(如果他真的要不保存就离开,或者让他保存,以及进行一些簿记,请咨询他)。 大多数答案都提到onbeforeunload事件。
就像是
$(window).on('beforeunload ',function() {
return 'Are you sure ?';
});
但这不是我真正需要的。 首先,它不允许我按照用户要求进行对话框。 对我来说,理想的方法是使用jQuery.UI模态对话框或类似的对话框,并带有自定义消息和两个清除按钮:“不保存就离开页面”和“保存并离开”此外,这仅控制是否可以离开页面,但我需要执行其他操作(例如,在某些页面中,允许用户静默保存信息并离开。。。不要问我,业务分析师决定这样做),因此,做这样的伪代码?
$(window).on('some event when I leave the page ', function() { destiny = DetectDestinyPage(); if (destiny in list of PagesToAsk and FormHasNotSavedInfo()){ answer = modalDialogAskingTheClientIfHeWantToSave() if(answer == "yes"){ ajaxCallToServerToSave() } } GoToDestiny(destiny) } }
希望这个能对您有所帮助
jQuery(window).bind('beforeunload',function(){
var Ans = confirm("Are you sure you leave this page!");
if(Ans==true){
return true;
}else{
return false;
}
});
您应该得到用户的确认是否正确; 您可以这样:
$(window).on('some event when I leave the page ', function() { let r=confirm("Are sure to leave?") // if user click 'yes', r=true, else r=false if(r){ // your code } } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.