按下按钮时确认的 JavaScript 警报框

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

JavaScript alert box with confirm on button press

javascriptalert

提问by Cameron

I have this link:

我有这个链接:

<p id="accept-favor"><a title="Accept this Favor" href="?wp_accept_favor=<?php comment_ID(); ?>">Accept this Favor</a></p>

<p id="accept-favor"><a title="Accept this Favor" href="?wp_accept_favor=<?php comment_ID(); ?>">Accept this Favor</a></p>

I want to show a JavaScript alert box when a user clicks it saying: "Are you sure you would like to accept this reply as your favor?" with two buttons one saying "Yes" which will allow the function to run and the other saying "No" which will just cancel the postback and keep the user on the page.

我想在用户点击它时显示一个 JavaScript 警告框说:“你确定你愿意接受这个回复作为你的帮助吗?” 有两个按钮,一个说“是”,这将允许该功能运行,另一个说“否”,这将取消回发并将用户保留在页面上。

How would I do this? Thanks :)

我该怎么做?谢谢 :)

回答by Kaleb Brasee

You can easily do it with a confirm onclick:

您可以通过点击确认轻松完成:

<p id="accept-favor"><a title="Accept this Favor" 
  href="?wp_accept_favor=<?php comment_ID(); ?>" 
  onclick="return confirm('Are you sure you would like to accept this reply as your favor?');"
  >Accept this Favor</a></p>

Though this will say OK/Cancel instead of Yes/No. If you really want Yes/No, you'll have to use a custom dialog.

虽然这会说 OK/Cancel 而不是 Yes/No。如果您真的想要是/否,则必须使用自定义对话框。

回答by SLaks

You can write onclick="return confirm('Are you sure?');".

你可以写onclick="return confirm('Are you sure?');"

The confirmfunctionshows an OK / Cancel dialog and returns trueif the user clicked OK.
returning falsefrom an onclickhandler will cancel the default action of the click.

confirm函数显示一个确定/取消对话框,并true在用户单击确定时返回。来自处理程序的
returning将取消点击的默认操作。falseonclick

回答by sailaja.p

You can use this function:

您可以使用此功能:

myFunction() {
     var x;
     if (confirm("Are you sure?") == true) {
         x = "You pressed OK!";
     } else {
         x = "You pressed Cancel!";
     }
     return x; 
}
myFunction();

回答by jave.web

As other answers talk about direct onclick, I would like to present a solution for a "nicer" (IMO=in my opinion) version using the addEventListenerand preventDefaultmethods. Because this way you could bind more clickhandlers.

当其他答案谈论直接onclick 时,我想提出一个使用addEventListenerpreventDefault方法的“更好”(IMO = 在我看来)版本的解决方案。因为这样你可以绑定更多的点击处理程序。

HTML

HTML

<a href="#" id="confirmClickActionElementId">click me</a>

JavaScript

JavaScript

document
    .getElementById("confirmClickActionElementId")
    .addEventListener("click", function( e ){ //e => event
        if( ! confirm("Do you really want to do this?") ){
            e.preventDefault(); // ! => don't want to do this
        } else {
            //want to do this! => maybe do something about it?
            alert('Ok, lets do this!');
        }
    });

Fiddle: http://jsfiddle.net/ouyf86ya/

小提琴:http: //jsfiddle.net/ouyf86ya/

...or the old "return" way:

...或旧的“ return”方式:

document
    .getElementById("confirmClickActionElementId")
    .addEventListener("click", function(  ){ 
        return confirm("Do you really want to do this?") ;
    });

Fiddle: http://jsfiddle.net/y2jLpkbb/

小提琴:http: //jsfiddle.net/y2jLpkbb/