有没有一种方法可以在jQuery中捕获警告框的“确定”按钮单击事件?
alert()
函数是同步
的,您无法验证点击了什么(它不返回任何内容),因此在调用之后,下面的代码将被执行并关闭(ok或close按钮)。警告框不用于获取用户输入。它是一条提醒消息。如果您需要检查用户想要什么,应该使用 confirm()
。请注意,函数名称像警告一样反映了其用途。
例如:
// 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.
}
警告是一个阻塞函数,这意味着,如果你不关闭它,下面的代码将不会执行。因此,你不需要捕获警告关闭事件,只需在警告下面编写代码,在警告窗口关闭时,下面的代码将自动执行。
请参见下面的示例:
alert("Close Me");
// Write down the code here, which will executed only after the alert close
console.log("This code is executed after alert")
免责声明:这是一个非常糟糕的做法。
技术上你可以使用以下代码来钩入它:
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");
.call
,它是否可以在IE中工作,因为它会隐式地成为window
的一个方法...编辑:对我来说,在IE7上确实可以工作,哈哈。 - Esailijawindow.alert()没有事件。基本上,当用户单击“确定”后,它被调用的下一行代码将会执行。我不确定为什么需要监听它。
alert()
的本地提示,confirm()
,它会给出一个布尔响应。我猜测 OP 使用了错误的术语。 - Jared Farrish我在我创建的一个网站上尝试了这个方法,它完美地运行了:
<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>
jAlert("Alert message goes here.");
$('#popup_ok').bind('click',function(){
//Do operation after clicking ok button.
function_do_operation();
});
confirm()
Javascript 模态提示框吗? - Jared Farrishalert()
被关闭后代码会继续执行。因此,你可以隐式地知道它已被点击。 - Pointy