一旦我触发了event . preventdefault(),我如何再次恢复默认操作?


当前回答

好的!它适用于点击事件:

$("#submit").click(function(event){ 
  
   event.preventDefault();

  // -> block the click of the sumbit ... do what you want

  // the html click submit work now !
  $("#submit").unbind('click').click(); 

});

其他回答

我使用了以下代码。这对我来说很有效。

$('a').bind('click', function(e) {
  e.stopPropagation();
});

为了执行异步调用,我不得不在jQuery中延迟表单提交。下面是简化的代码…

$("$theform").submit(function(e) {
    e.preventDefault();
    var $this = $(this);
    $.ajax('/path/to/script.php',
        {
        type: "POST",
        data: { value: $("#input_control").val() }
    }).done(function(response) {
        $this.unbind('submit').submit();
    });
});

好的!它适用于点击事件:

$("#submit").click(function(event){ 
  
   event.preventDefault();

  // -> block the click of the sumbit ... do what you want

  // the html click submit work now !
  $("#submit").unbind('click').click(); 

});

正如@Prescott所评论的,相反的是:

evt.preventDefault();

可能是:

本质上等同于'做违约',因为我们不再阻止它。

否则,我倾向于让你看看其他评论和答案提供的答案:

如何解除调用event.preventDefault()的监听器(使用jQuery)?

如何重新启用event.preventDefault?

请注意,第二个已经接受了一个由redsquare给出的示例解决方案(在这里发布了一个直接解决方案,以防这不是封闭的重复):

$('form').submit( function(ev) {
     ev.preventDefault();
     //later you decide you want to submit
     $(this).unbind('submit').submit()
});

这不是对问题的直接回答,但它可能会帮助到一些人。我的观点是,你只根据某些条件调用preventDefault(),因为如果你对所有情况调用preventDefault(),就没有必要有一个事件。因此,只有在条件满足时才使用if条件和调用preventDefault(),才能在其他情况下以通常的方式运行该函数。

$('.btnEdit').click(function(e) {

   var status = $(this).closest('tr').find('td').eq(3).html().trim();
   var tripId = $(this).attr('tripId');

  if (status == 'Completed') {

     e.preventDefault();
     alert("You can't edit completed reservations");

 } else if (tripId != '') {

    e.preventDefault();
    alert("You can't edit a reservation which is already attached to a trip");
 }
 //else it will continue as usual

});