重新绑定分配给ajax链接的操作

Rebind action assigned to ajax link

本文关键字:ajax 链接 操作 分配 新绑定 绑定      更新时间:2023-11-30

有一个链接可以删除帖子:

<a id="post_232_destroy" class="postDestroy" rel="nofollow" data-remote="true" data-method="delete" data-confirm="Are you sure?" href="/someurl">Delete</a>

javascript(从coffescript编译):

function() {
  jQuery(function() {
    return $("a.postDestroy").bind("ajax:success", function(event, data) {
      $('#post_' + data.post_id).remove();
    }).bind("ajax:error", function() {
      alert('Please try again');
    });
  });
}).call(this);

我正在通过ajax添加新的帖子,所以对于最近添加的每个帖子,都缺少"绑定到删除"按钮。Post被删除,但是ajax:success没有被调用,所以div没有被删除。我怎样才能再次绑定它?

每次添加新帖子时,您都可以解除所有帖子的绑定,然后再次绑定。

$("a.postDestroy").unbind("ajax:success");
$("a.postDestroy").unbind("ajax:error");
$("a.postDestroy").bind("ajax:success", function(event, data) {
  $('#post_' + data.post_id).remove();
}).bind("ajax:error", function() {
  alert('Please try again');
});
});

编辑:尝试使用jquery"live"函数而不是"bind":

$("a.postDestroy").live("ajax:success", function(event, data) {
  $('#post_' + data.post_id).remove();
}).live("ajax:error", function() {
  alert('Please try again');
});
});