简体   繁体   English

除非用户单击特定的按钮,否则如何向用户显示警报框

[英]How can I display alert box to users unless they click a specific button

I have set up a page with a form that users can make changes to using PHP. 我已经建立了一个表单,用户可以使用PHP进行更改。 When they try to navigate away from the page or refresh, an alert box appears using JS. 当他们尝试离开页面或刷新时,使用JS出现一个警告框。 However when they click the Save button the alert box still appears. 但是,当他们单击“保存”按钮时,仍会出现警告框。

Is there any way I can stop the alert box appearing when the user clicks on the save button? 当用户单击“保存”按钮时,有什么方法可以阻止警报框出现?

here is my 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" /> 

If I understand correctly, you don't want to show the confirmation dialog when someone clicks the save button right? 如果我理解正确,那么当有人单击“保存”按钮时,您不想显示确认对话框吗? Why not just deregister the onbeforeunload method in that click handler like so: 为什么不像这样在单击处理程序中注销onbeforeunload方法:

window.onbeforeunload = confirmExit; //By default assign the confirmExit

//If user clicks on save, just set it to null.
$('#save').click function(){
 window.onbeforeunload = null;
}

This way, you don't need to maintain a separate variable called needToConfirm . 这样,您无需维护一个名为needToConfirm的单独变量。 Also, try to understand the way javascript executes your code. 另外,请尝试了解javascript执行代码的方式。 It does it line by line. 它逐行完成。 So, your needToConfirm defined inside the click handler right now gets set to false when the user clicks save. 因此, 用户单击保存 ,在单击处理程序中定义的needToConfirm现在将设置为false But even before that callback is called, you already have bound the onbeforeunload event as the default value of needToConfirm is true . 但即使在调用该回调之前,您也已经绑定了onbeforeunload事件,因为needToConfirm的默认值为true

Try to also keep in mind the scoping of variables in javascript. 尝试也要记住javascript中变量的作用域。 If you redefine variable needToConfirm inside a click handler it would not necessarily access the "global" variable you intend to share across different functions. 如果在点击处理程序中重新定义变量needToConfirm则不一定访问要在不同功能之间共享的“全局”变量。 And ofcourse, like other people pointed out, don't use the same id for different HTML elements. 当然,就像其他人指出的那样,不要对不同的HTML元素使用相同的id It is not supposed to be used like that. 它不应该那样使用。

First of all, you are not executing your conditional code inside the if statement. 首先,您没有在if语句中执行条件代码。 It is out of it, fix that and try again. 没问题了,请解决此问题,然后重试。 If it still doesn't work then try the following: 如果仍然无法使用,请尝试以下操作:

The page get refreshed before the $("#save").click() returns anything (in this case, needToConfirm = false . Therefore the alert box appears as usual. You have to modify your html as follows: $("#save").click()返回任何内容之前刷新页面(在这种情况下, needToConfirm = false 。因此,警告框照常显示。您必须按如下方式修改html:

<input type="button" id="save" />

and use javascript to actually submit the form... You can do that as follows: 并使用javascript实际提交表单...您可以按照以下步骤进行操作:

$("#save").click(function() { var needToConfirm = false; $("#idOfForm").submit(); return false; }

Also, change ID of one of the buttons as 2 elements can never have the same ID... or use class instead! 另外,将按钮之一的ID更改为2个元素永远不能具有相同的ID ...或改用class!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM