拦截按钮上的点击事件,要求确认,然后继续 [英] Intercept click event on a button, ask for confirmation, then proceed

查看:74
本文介绍了拦截按钮上的点击事件,要求确认,然后继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于变量SomeCondition,我需要拦截按钮上的click事件,并要求确认,如果他们说确定,请继续,否则请忽略单击.

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.

所以像这样:

if(SomeCondition) {

// disable click on button

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

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

}

注意:button1已通过javascript连接到我无法更改的外部.js文件中,并通过click事件进行了连接.

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

我也不知道click事件绑定了什么,因此如果SomeCondition为true,则必须禁用该单击,然后要求确认,然后继续单击.

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.

推荐答案

确定您的window.confirm按钮功能内

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

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

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

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.

有点鸡/蛋的问题.

在阅读完已编辑的问题后:

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

这篇关于拦截按钮上的点击事件,要求确认,然后继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆