JavaScript 警报框,按下按钮时进行确认

发布于 2024-10-16 20:41:36 字数 274 浏览 4 评论 0原文

我有这个链接:

接受此恩惠

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

我该怎么做?谢谢 :)

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>

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.

How would I do this? Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

古镇旧梦 2024-10-23 20:41:36

您可以通过单击确认轻松完成此操作:

<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>

尽管这会显示“确定/取消”而不是“是/否”。如果您确实想要“是/否”,则必须使用自定义对话框。

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.

傾旎 2024-10-23 20:41:36

您可以编写 onclick="return recognize('Are you certain?');"

confirm 函数显示“确定/取消”对话框如果用户单击“确定”,则返回 true
onclick 处理程序中返回 false 将取消默认的点击操作。

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

The confirm function shows an OK / Cancel dialog and returns true if the user clicked OK.
returning false from an onclick handler will cancel the default action of the click.

樱花坊 2024-10-23 20:41:36

您可以使用此功能:

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

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();
薄情伤 2024-10-23 20:41:36

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

HTML

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

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!');
        }
    });

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

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

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

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

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

HTML

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

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/

...or the old "return" way:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文