当我点击 remove 按钮数据已删除,但无法删除 tr 如表所示。
remove
tr
<tr> <td>name</td> <td><button id="removebutton" data-id="?">Remove</button></td> </tr>
我想选择 tr公司 在顶部并将其从ajax响应中移除。
tr公司
我试过:
$("#removebutton).on("click",function(){ success: function(res){ $(this).parents("tr").remove(); //this doesn't work } });
您可以使用:
$(this).parent().remove(); // or $(this).closest('tr').remove();
如果有用请告诉我:)
编辑
我很想知道为什么我投了反对票:)
不能在多行上使用同一ID。
<button class="removebutton" data-id="?">Remove</button>
那么这个 $("#removebutton). 应该是 $(".removebutton").
$("#removebutton).
$(".removebutton").
你错过了关门 " 上面是你的选择器。
"
尝试使用 arrow function 相反。这将确保作用域保留在click事件而不是 success()
arrow function
success()
$("#removebutton").on("click", function() { ..... success: (res) => { $(this).parent().remove(); } .... });