我找到了很多解决方案,但没有一个能真正解决我的问题。如果用户决定离开而不保存他的更改,我必须在页面上执行某些操作(询问他是否真的想离开而不保存,或者让他保存它们,以及一些簿记)。 大多数答案都提到了 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;
}
});
关于javascript - 离开页面前的对话操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53038136/