javascript 如何使用javascript一键显示确认警报?

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

How to show the confirmation alert with one button using javascript?

javascript

提问by Lavanya

In my application, i need to show the message in alert with OK button.

在我的应用程序中,我需要使用 OK 按钮在警报中显示消息。

In that button click event, i want to do some functionalities using the javascript.

在该按钮单击事件中,我想使用 javascript 执行一些功能。

So, how can i do this in my app?

那么,我怎样才能在我的应用程序中做到这一点?

Please let me know?

请告诉我?

回答by James Allardice

I'm not entirely sure what you are trying to do, but you may be looking for the window.confirmfunction, which is a built-in function like alertthat allows you to capture the response (true or false) from the user:

我不完全确定您要做什么,但您可能正在寻找该window.confirm函数,它是一个内置函数alert,允许您捕获用户的响应(真或假):

if(confirm("Some message")) {
    //Clicked ok
} else {
    //Clicked cancel
}

If you want more functionality than that I'm afraid you'll have to look elsewhere. There are endless modal scripts and libraries available, so just search for one that suits your needs.

如果您想要更多的功能,恐怕您将不得不寻找其他地方。有无数可用的模态脚本和库,因此只需搜索适合您需求的脚本和库即可。



Having re-read your question title, maybe you just want the normal window.alertfunction? That will display a dialog with one button, and, in general, prevent execution of the code following it until the user has closed it:

重新阅读您的问题标题后,也许您只想要正常window.alert功能?这将显示一个带有一个按钮的对话框,并且通常会在用户关闭它之前阻止执行它后面的代码:

alert("Some message");

回答by Angel

If you'll be using alert() then the execution of javascript will stop where the alert is written. And after the OK button is pressed, the code execution will resume. So, do an alert(message); and after that line, put the code you want to be executed after the OK button is pressed.

如果您将使用 alert(),那么 javascript 的执行将在写入警报的地方停止。按下确定按钮后,代码将继续执行。所以,做一个 alert(message); 在该行之后,放置您想要在按下 OK 按钮后执行的代码。

If you want more control, you can use jQuery UI Dialog.

如果你想要更多的控制,你可以使用 jQuery UI Dialog。

回答by freakish

Forget about built-in popups. Even if you make it work you'll probably hit some cross-browser issues and you won't be able to sleep (trust me: I know what I'm saying, I've been there). :)

忘记内置弹出窗口。即使你让它工作,你也可能会遇到一些跨浏览器的问题,你将无法入睡(相信我:我知道我在说什么,我去过那里)。:)

I've been using jQuery UI ( link) for some time and I am really satisfied with. Give it a try!

我已经使用 jQuery UI(链接)有一段时间了,我真的很满意。试一试!

回答by ClydeFrog

Here's a simple example

这是一个简单的例子

if (confirm("Select a button")){
    alert("You selected OK");
}else{
    alert("You selected Cancel");
}

But if you're just looking for a popup with one button I would go for the alert()

但是如果你只是在寻找一个带有一个按钮的弹出窗口,我会选择 alert()

alert("Press OK to close!");

For more popup examples, go to w3schools

有关更多弹出示例,请访问 w3schools