gpt4 book ai didi

jquery - 拦截按钮上的点击事件,要求确认,然后继续

转载 作者:行者123 更新时间:2023-12-03 21:41:56 24 4
gpt4 key购买 nike

基于变量SomeCondition,我需要拦截按钮上的点击事件,并要求确认,如果他们说“确定​​”,则继续,否则忽略点击。

所以类似:

if(SomeCondition) {

// disable click on button

var isOk = window.confirm("Are you sure?");

if(isOk) {
$("#button1").click();
}

}

注意:button1 已经通过 javascript 从外部 .js 文件连接了点击事件,但我无法更改。

我也不知道点击事件绑定(bind)了什么,所以如果 SomeCondition 为 true,我必须禁用点击,然后要求确认,然后继续点击。

最佳答案

在按钮的功能内处理 isOK 您的窗口。确认

$('#button1').click(function(){ 
if(window.confirm("Are you sure?"))
alert('Your action here');
});

您将遇到的问题是,当您触发“您确定吗”时,点击已经发生,如果是启动原始 window.confirm 的点击,则调用 PreventDefault 不会停止原始点击的执行。

有点先有鸡还是先有蛋的问题。

编辑:阅读您编辑的问题后:

    var myClick = null;

//get a list of jQuery handlers bound to the click event
var jQueryHandlers = $('#button1').data('events').click;

//grab the first jquery function bound to this event
$.each(jQueryHandlers,function(i,f) {
myClick = f.handler;
return false;
});

//unbind the original
$('#button1').unbind('click');

//bind the modified one
$('#button1').click(function(){
if(window.confirm("Are You Sure?")){
myClick();
} else {
return false;
}
});

关于jquery - 拦截按钮上的点击事件,要求确认,然后继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4674991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com