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

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

Intercept click event on a button, ask for confirmation, then proceed

jquery

提问by Blankman

Based on a variable SomeCondition, I need to intercept a click event on a button, and ask for confirmation, if they say ok, proceed, otherwise ignore click.

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

So something like:

所以像:

if(SomeCondition) {

// disable click on button

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

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

}

Note: button1 has already been wired up with a click event via javascript from an external .js file that I can't change.

注意:button1 已经通过 javascript 从我无法更改的外部 .js 文件中连接了一个单击事件。

I don't know what the click event was bound too, so I have to disable the click if SomeCondition is true, then ask for confirmation, then continue with the click.

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

回答by Jason Benson

Process isOK your window.confirm within the function of the button

过程是OK你的window.confirm函数内的按钮

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

The issue you're going to have is the click has already happened when you trigger your "Are You Sure" Calling preventDefault doesn't stop the execution of the original click if it's the one that launched your original window.confirm.

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

Bit of a chicken/egg problem.

有点鸡/蛋问题。

Edit: after reading your edited question:

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

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

回答by Skilldrick

With jQuery to prevent default you just return false:

使用 jQuery 来防止默认,您只需return false

$("#button1").click(function () {
    //do your thing
    return false;
});

回答by Abhijith C R

To anyone who is looking for a simple solution (not particular to the special condition mentioned here)

对于任何正在寻找简单解决方案的人(不是特别针对这里提到的特殊情况)

Just add this attribute to the button:

只需将此属性添加到按钮:

.... , onclick = "return confirm('are you sure?')"

When the confirmation box returns a false, the click event gets cancelled. Note: The onclick handler explicitly mentioned inline will override any other bindings.

当确认框返回 false 时,单击事件将被取消。注意:内联明确提到的 onclick 处理程序将覆盖任何其他绑定。

回答by GiorgosK

I could not get any of the answers above to work using jquery 1.9.1

我无法使用 jquery 1.9.1 获得上述任何答案

here is what worked for a previously set clickevent

这是对先前设置的click事件有效的方法

var elem = $('#button1');
var oldClick = $._data(elem[0], 'events').click[0].handler;
elem.off('click');

// Pass the original event object.
elem.click(function(e){
  if(window.confirm("Are You Sure?")){
    oldClick(e);
  } else {
    return false;
  }
});   

very similar to Jason Benson's answer above

与上面的 Jason Benson 的回答非常相似