【发布时间】:2017-03-18 10:04:45
【问题描述】:
我已经设置了一个页面,其中包含一个用户可以使用 PHP 进行更改的表单。当他们尝试离开页面或刷新时,会出现一个使用 JS 的警告框。但是,当他们单击“保存”按钮时,警报框仍然会出现。
当用户点击保存按钮时,有什么方法可以阻止出现警告框?
这是我的 JS:
var needToConfirm = true;
$('#save').click(function(){
var needToConfirm = false;
})
if (needToConfirm == true)
window.onbeforeunload = confirmExit;
function confirmExit()
{
return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Here is my HTML (just the button): -->
<input type="submit" id="save" />
<input type="hidden" id="save" name="submitted" value="TRUE" />
【问题讨论】:
-
你不能有两个具有相同 id 的元素。 ID 在整个页面中必须是唯一的。
标签: javascript jquery html