Javascript 有没有办法捕获警报确定按钮单击事件?

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

Is there a way to capture the alert ok button click event?

javascriptjquery

提问by Aperture

Is there a way to capture the alert ok button click event? In jQuery?

有没有办法捕获警报确定按钮单击事件?在 jQuery 中?

回答by davidbuzatto

The alert()function is synchronousand you can't verify what was clicked (it does not return anything), so the code below the call will be executed after it is closed (ok or close button). The alert is not used to gain user input. It is an alert, a message to the user. If you need to check what the user want, you should use confirm(). Note that the function name tells its purpose like alert.

alert()函数是synchronous并且您无法验证单击了什么(它不返回任何内容),因此调用下面的代码将在关闭(确定或关闭按钮)后执行。警报不用于获取用户输入。它是一个警报,是给用户的消息。如果你需要检查用户想要什么,你应该使用confirm(). 请注意,函数名称告诉它的用途,就像警报一样。

Something like:

就像是:

// if the ok button is clicked, result will be true (boolean)
var result = confirm( "Do you want to do this?" );

if ( result ) {
    // the user clicked ok
} else {
    // the user clicked cancel or closed the confirm dialog.
}

回答by Esailija

Disclaimer:This is a very bad thing to do.

免责声明:这是一件非常糟糕的事情。

Technically you could hook into it with this code:

从技术上讲,您可以使用以下代码挂钩:

window.alert = function(al, $){
    return function(msg) {
        al(msg);
        $(window).trigger("okbuttonclicked");
    };
}(window.alert, window.jQuery);



$(window).on("okbuttonclicked", function() {
    console.log("you clicked ok");
});

alert("something");

?

?

Demo: http://jsfiddle.net/W4d7J/1/

演示:http: //jsfiddle.net/W4d7J/1/

回答by We'wake

Alert is a blockingfunction, means, if you don't close it, the code below will not execute. So you don't have to capture the alert close event, just write down the code below that alert, when alert window will be closed the code below will be executed automatically.

Alert 是一个阻塞函数,意思是,如果你不关闭它,下面的代码将不会执行。所以你不必捕获警报关闭事件,只需写下警报下面的代码,当警报窗口关闭时,下面的代码将自动执行。

See example below:

请参阅下面的示例:

alert("Close Me");
// Write down the code here, which will executed only after the alert close

回答by epascarello

There is no event for the window.alert(). Basically the next line after it is called when they click ok. I am not sure why you would need to listen for it.

window.alert() 没有事件。基本上是当他们单击确定时调用后的下一行。我不知道你为什么需要听它。

回答by ALLE

You could use JAlert and assign a click handler to the ok button.

Something like.

您可以使用 JAlert 并为 ok 按钮分配一个点击处理程序。

就像是。

     jAlert("Alert message goes here."); 
    $('#popup_ok').bind('click',function(){
           //Do operation after clicking ok button.
             function_do_operation();

    });

回答by AppEmmanuel

I tried this in a site I created and it worked perfectly :

我在我创建的网站中尝试了这个,它工作得很好:

<a href="javascript:var rslt=confirm('Cancel Appointment Creation');if(rslt){window.open('http://www.the_website_I_want_to_navigate_to.com')}else{}"> << Back </a>