最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - jQuery Remove table row on $.ajax success - Stack Overflow

programmeradmin3浏览0评论

I need to remove a table row after a successful ajax call, not sure how to do it. 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>

I need to remove a table row after a successful ajax call, not sure how to do it. 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>
Share Improve this question asked Dec 4, 2012 at 6:16 savagenoobsavagenoob 4139 silver badges26 bronze badges 3
  • your javascript and html looks solid (Sean's answer has a nice structure though). your problem may be something else entirely; on the success function, put a line to alert(html);. also add an error: function(html) { alert(html); } also. – rkw Commented Dec 4, 2012 at 8:02
  • @rkw it returns the echo from the php "true". its pleting the mysql query on the back end but its not executing $(this).closest('tr').remove(); – savagenoob Commented Dec 5, 2012 at 2:55
  • append return false; to your onclick statement; to prevent any postback that may occur. inside your success function, use the code from Adil. If that doesn't work, instead of the code from Adil, put in console.log($(this).closest('tr')) and see what object the console returns – rkw Commented Dec 5, 2012 at 6:31
Add a ment  | 

5 Answers 5

Reset to default 2

You can use closest() to find out the row containing the clicked button and use remove() to remove the row of button.

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

Onclick is so 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.

<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(); declared. Now after successful ajax response use parent.parent().remove(); This would be easy and understandable too.

use remove().

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

Have a look at Jquery remove - it will do the trick

发布评论

评论列表(0)

  1. 暂无评论