javascript/jquery:响应用户在警报对话框中单击“确定”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7275512/
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
javascript/jquery: responding to a user clicking "ok" on an alert dialog
提问by shealtiel
my code:
我的代码:
alert('Some message');
Question 1:
问题 1:
How to execute code that comes after alert()
when user finished interacting with alert box?
如何执行alert()
用户完成与警报框交互后的代码?
Question 2:
问题2:
How to detect if user pressed OK
or Cancel
on alert box ?
如何检测用户是否按下OK
或Cancel
打开警报框?
回答by Darin Dimitrov
Question 1:
问题 1:
The alert
method blocks execution until the user closes it:
该alert
方法阻止执行,直到用户关闭它:
alert('Some message');
alert('doing something else after the first alert is closed by the user');
Question 2:
问题2:
use the confirm
function:
使用confirm
函数:
if (confirm('Some message')) {
alert('Thanks for confirming');
} else {
alert('Why did you press cancel? You should have confirmed');
}
回答by Nick Shaw
The code after the alert()
call won't be executed until the user clicks ok to the alert, so just put the code you need afterthe alert()
call.
alert()
调用后的代码不会执行,直到用户单击“确定”以提醒警报,因此只需在调用后放置您需要的代码即可alert()
。
If you want a nicer floating dialog than the default javascript confirm()
popup, see jQuery UI: floating window
如果您想要一个比默认 javascriptconfirm()
弹出窗口更好的浮动对话框,请参阅jQuery UI:浮动窗口
回答by rlemon
var r = confirm("Press a button!");
if (r == true) {
alert("You pressed OK!");
}
else {
alert("You pressed Cancel!");
}