当用户更改了表单中的某些内容,然后单击任何将把他定向到另一个页面的链接时,我想触发一个弹出窗口,其中包含“你想在离开前保存吗?”选项。
我该怎么做?
最佳答案
例子:
<script type="text/javascript">
var shouldConfirm = false;
window.onbeforeunload = function() {
if(shouldConfirm) {
return "You have made unsaved changes. Would you still like to leave this page?";
}
}
</script>
<input id="FullName" type="text" />
<script type="text/javascript">
document.getElementById('FullName').onchange = function() {
shouldConfirm = true;
}
</script>
在 4GuysFromRolla.com 有一篇完整的文章.
关于javascript - 如何在离开当前页面之前触发与JS的对话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/514776/