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

后端 未结 4 940
陌清茗
陌清茗 2020-12-08 10:33

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.

4条回答
  •  感情败类
    2020-12-08 11:18

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

    here is what worked for a previously set click event

    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

提交回复
热议问题