使用jQuery删除行表

Adi*_*ing 5 jquery

假设我有一个这样的表

id  name    address     action
--------------------------------------
s1  n1  a1      delete
s2  n2  a2      delete
Run Code Online (Sandbox Code Playgroud)

例如,删除是一个链接<a href="http://localhost/student/delete/1">.在实际案例中,我使用ajax删除学生.为了简化代码,我只是提醒链接并省略ajax脚本.我只想知道如何使用jquery从html文档中删除行.

$(document).ready(function() {
$("a").click(function(event) {
    alert("As you can see, the link no longer took you to jquery.com");
        var href = $(this).attr('href');
        alert(href);
        event.preventDefault();
   });
);
Run Code Online (Sandbox Code Playgroud)

我想,在我提醒链接后,所选行将自动删除.有什么建议如何实现这个?

Phi*_*ert 17

您不需要调用preventDefault().简单地从事件处理程序返回false具有相同的效果.

要删除<a>链接所在的行,可以调用$(this).closest("tr").remove():

$(document).ready(function() {
$("a").click(function(event) {
    alert("As you can see, the link no longer took you to jquery.com");
    var href = $(this).attr('href');
    alert(href);
    $(this).closest("tr").remove(); // remove row
    return false; // prevents default behavior
   });
);
Run Code Online (Sandbox Code Playgroud)