Javascript:检测警报框何时被确定和/或关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7146028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:02:22  来源:igfitidea点击:

Javascript: Detect when an alert box is OK'ed and/or closed

javascriptjquery

提问by oshirowanen

How can I detect when a javascript alert box is OK'ed and/or closed?

如何检测 javascript 警报框何时被确定和/或关闭?

回答by Quentin

Since alertis blocking:

由于alert阻塞:

alert('foo');
function_to_call_when_oked_or_closed();

Just put the function after the call to alert.

只需将函数放在调用alert.

回答by Jules

You can show a confirm boxthat displays a message with an OK and a Cancel button and check which button the user clicked by:

您可以显示一个confirm box带有 OK 和 Cancel 按钮的消息,并检查用户单击了哪个按钮:

<script type="text/javascript">

var answer = confirm ("Is this working for you?")
if (answer)
    alert ("Woo Hoo! Then my answer was correct.")
else
    alert ("Darn. Well, keep trying then.")

</script>

If you want to make use of a simple alert boxyou can have a look at 1001 tutorials online such as thisone for example. But your question does not specify how exactly you want to implement your alert.

如果您想使用一个简单的警报框,您可以在线查看 1001 个教程,例如这个。但是你的问题没有具体说明你想如何实现你的alert.

http://www.tizag.com/javascriptT/javascriptalert.php

http://www.tizag.com/javascriptT/javascriptalert.php

回答by Robby Shaw

If user didn't press ok or close, the JS will not proceed to the next line. So actually there is no need to detect this.

如果用户没有按确定或关闭,JS 将不会进行到下一行。所以实际上没有必要检测到这一点。

Maybe you want to know if the user pressed ok or close. For the alert popup box, there is no easy way to tell it is ok'ed or closed. In Firefox, you don't even have the close button.

也许您想知道用户是按确定还是关闭。对于警告弹出框,没有简单的方法来判断它是正常还是关闭。在 Firefox 中,您甚至没有关闭按钮。

So if you really want to do this, you can use Confirm Box or Prompt Box. Please check the link below for how to use Confirm Box or Prompt Box: http://www.w3schools.com/js/js_popup.asp

所以如果你真的想这样做,你可以使用确认框或提示框。请查看以下链接以了解如何使用确认框或提示框:http: //www.w3schools.com/js/js_popup.asp