在 XmlHttpRequest 完成后删除表行

removing a table row after XmlHttpRequest complete

本文关键字:删除 XmlHttpRequest      更新时间:2023-09-26

我有一个HTML表格,里面有一堆表格行,每个表格都有一个id属性和一个表格单元格中的删除按钮。我将 ajax 调用绑定到删除按钮:

$('.deletebutton').bind 'click', () ->
  id = $(this).attr "id" // getting my row id here
  $.ajax // sending my ajax request
    url: "/admin/delete/#{id}"
    type: 'POST'
    beforeSend: (jqXHR, settings) ->
      jqXHR.setRequestHeader('X-CSRF-Token', $("meta[name='csrf-token']").attr('content'))

这没有任何问题。 现在我将以下参数添加到我的 ajax 调用中以删除表行,但它不起作用。 我尝试了不同的事情,但我无法弄清楚。 请帮忙。

    success: (data, textStatus, jqXHR) ->
      $('tr[id=id]').remove() // I don't understand why my jQuery selector doesn't work here...

在普通的JavaScript中('因为我不知道CoffeeScript - 对不起:-))。无需使用行"id"。

$('.deletebutton').click(function(){
   var rowToDelete = $(this).closest('tr');
    $.ajax({
       /* --- the rest of your params here --- */
        success: function() {
            rowToDelete.remove();
        }
    })
});