使用Jquery在数据库中删除项目后删除表格行

时间:2016-05-29 18:00:18

标签: jquery

我有一个删除按钮,用于删除数据库中的条目。我想在删除后隐藏该条目的表格行,但无法找到隐藏tr的方法。

我的代码是:

 $(document).on('click' , 'a.delete-dialog', function(){
    var id_hired_staff = $(this).closest('tr').attr('data-id_hired_staff');
  $("#dialog-confirm").data('del-id_hired_staff', id_hired_staff).dialog('open');
  return false;
});

var buttonsOpts = {}
buttonsOpts[Settings.fire_staff] = function() {
  var id_hired_staff = $(this).data('del-id_hired_staff');
  $.ajax({
    type: "POST",
    url: "/path/fire_staff",
    data: "id_hired_staff="+id_hired_staff,
    success: function(result){
      $("tr").find("[data-id_hired_staff='" + id_hired_staff + "']").hide(); // This is what I've tried
   }
  });
$(this).dialog('close');
}

1 个答案:

答案 0 :(得分:1)

我会把我的评论写成答案:

 var id_hired_staff;
 $(document).on('click' , 'a.delete-dialog', function(){
    id_hired_staff = $(this).closest('tr').attr('data-id_hired_staff');
  $("#dialog-confirm").data('del-id_hired_staff', id_hired_staff).dialog('open');
  return false;
});

var buttonsOpts = {}
buttonsOpts[Settings.fire_staff] = function() {
  var id_hired_staff = $(this).data('del-id_hired_staff');
  $.ajax({
    type: "POST",
    url: "/path/fire_staff",
    data: "id_hired_staff="+id_hired_staff,
    success: function(result){
      $("table").find("tr[data-id_hired_staff='" + id_hired_staff + "']").hide(); // This is what I've tried
   }
  });
$(this).dialog('close');
}

我将id_hired_staff作为全局变量,因此它将在click回调函数范围之外可用。并将选择器更改为我认为可行的选项