我已经用一个表单设置了一个页面,用户可以对使用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" />
发布于 2016-11-04 14:33:24
首先,在if语句中不执行条件代码。它出来了,修复它,然后再试一次。如果它仍然不起作用,那么尝试以下方法:
在$("#save").click()返回任何内容之前刷新页面(在本例中为needToConfirm = false )。因此,将像往常一样显示警报框。您必须按以下方式修改html:
<input type="button" id="save" />
使用javascript来提交表格..。你可以这样做:
$("#save").click(function() { var needToConfirm = false; $("#idOfForm").submit(); return false; }
另外,将其中一个按钮的ID更改为2个元素永远不会有相同的ID.或者用类代替!
https://stackoverflow.com/questions/40424917
复制相似问题