javascript 检查 OK 按钮被点击警告框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10185473/
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-10-26 08:57:25  来源:igfitidea点击:

Checking OK button is clicked on alert box

javascript

提问by Kichu

This the code for alerting some value:

这是用于提醒某些值的代码:

alert('Click the OK button Now !');

So now i want to check whether the OK button is clicked or not.

所以现在我想检查是否单击了确定按钮。

How can I do this using this JavaScript?

如何使用此 JavaScript 执行此操作?

回答by Paul Oliver

Confirm could work:

确认可以工作:

var r=confirm("Click the OK button now!");
if (r==true)
{
  alert("You pressed OK!");
}
else
{
  alert("You pressed Cancel!");
}

Confirm HAS to have an OK and Cancel button. If you only want one button, you should either use the alert()method (which doesn't tell you if the OKwas clicked) or you should look into something like the jQueryUI Dialogcontrol.

确认必须有一个确定和取消按钮。如果您只想要一个按钮,您应该使用该alert()方法(它不会告诉您是否OK单击了该按钮),或者您应该查看类似jQueryUI Dialog控件的内容。

The jQueryUI dialog is a bit more complicated because you need to include some extra JavaScript libraries and do a bit of extra wiring up to get it to work. There are a lot of examples to follow here.

jQueryUI 对话框有点复杂,因为您需要包含一些额外的 JavaScript 库并进行一些额外的连接以使其工作。有很多的例子来遵循这里

回答by DanS

Sounds like you might want a confirm box instead of an alert:

听起来您可能想要一个确认框而不是警报:

http://www.w3schools.com/js/js_popup.asp

http://www.w3schools.com/js/js_popup.asp

This returns true or false depending on what the user presses. Alert does not return a value.

这将根据用户按下的内容返回 true 或 false。警报不返回值。

回答by Scott

Use a jquery dialog then you can post or check what every you want from a range of buttons. Much more flexible

使用 jquery 对话框,然后您可以从一系列按钮中发布或检查您想要的内容。更灵活

Jquery Modal

jQuery 模态

回答by Scryptronic

As mentioned previously, confirm() is the best bet, however don't forget you can check which button was pressed, and ask for a value at the same time using prompt().

如前所述,confirm() 是最好的选择,但是不要忘记您可以检查按下了哪个按钮,并同时使用 prompt() 请求一个值。

if (prompt("Click the OK button?")!=null)
{
alert('you clicked OK and entered a value')
}
else
{
alert('you clicked cancel')
}