首页 > 解决方案 > 如何使用 jquery 选择器删除 tr?

问题描述

当我单击remove按钮时,数据被删除,但我无法删除tr表中显示的数据。

<tr>

<td>name</td>
<td><button id="removebutton" data-id="?">Remove</button></td>
</tr>

我想选择tr顶部并将其从 ajax 响应中删除。

我努力了:

$("#removebutton).on("click",function(){
  success: function(res){
  $(this).parents("tr").remove(); //this doesn't work
  }
});

标签: jquery

解决方案


尝试arrow function改用。这确保范围保持在点击事件而不是success()

$("#removebutton").on("click", function() {
  .....
  success: (res) => {
    $(this).parent().remove(); 
  }
  ....
});

推荐阅读