简体   繁体   English

jQuery在$ .ajax成功时删除表行

[英]jQuery Remove table row on $.ajax success

I need to remove a table row after a successful ajax call, not sure how to do it. 成功的ajax调用后,我需要删除表行,不确定如何去做。 Here is my code: 这是我的代码:

function closelead(rowid){
            var rowid1 = rowid;
            alert(rowid1);
            var parent = $(this).parent();
            $.ajax({
                type: "POST",
                url: "ajax/close.php",
                data: "rowid="+ rowid1,

                success: function(html){


                }
            });

            }

<tr><td><input type="button" onclick="closelead(<?php echo $leadlist['ID'];?>)" value="Close" class="searchbutton" /></td></tr>

You can use closest() to find out the row containing the clicked button and use remove() to remove the row of button. 您可以使用closest()查找包含单击的按钮的行,并使用remove()删除按钮的行。

success: function(html){
       $(this).closest('tr').remove();
}

Onclick is so 1999... Onclick就是1999年...

$('#closelead').on('click', function() {
    var $row = $(this).parent().parent();
    var rowid = $(this).data("row-id");
    $.ajax({
        type: "POST",
        url: "ajax/close.php",
        data: "rowid=" + rowid,
        success: function() {
            $row.remove();
        }
    });
});​

Change HTML to this. 将HTML更改为此。

<tr><td><button id="closelead" class="searchbutton" data-row-id="<?php echo $leadlist['ID'];?>">Close</button></td></tr>

DEMO 演示

Look in your code you already have var parent = $(this).parent(); 在您的代码中查看您已经有var parent = $(this).parent(); declared. 宣布。 Now after successful ajax response use parent.parent().remove(); 现在,在成功执行ajax响应之后,请使用parent.parent().remove(); This would be easy and understandable too. 这也将是容易并且可以理解的。

使用remove().

$(this).closest('tr').remove();

看看jQuery的删除 -它会做的伎俩

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM