当用户在表单中更改了内容,并点击任何链接以将其引导到另一页时,我想触发一个弹出窗口,询问是否要在离开前保存。
我该如何做到这一点?
我该如何做到这一点?
示例:
<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 上有一篇完整的文章。
这是一种实现方式,但并不总是可靠的:
<html>
<head>
<script type="text/javascript">
function leaving()
{
if(confirm("Would you like to save?"))
{
//Save info
}
else
{
//Don't save
}
}
</script>
</head>
<body onUnload="leaving()">
<!--Stuff-->
</body>
</html>