jQuery basic: How can i remove a table row when a button of this row is clicked?

前端 未结 3 1663
轻奢々
轻奢々 2020-12-31 02:48
$(\"#tableid tbody:last\").append(html);

This creates table rows dynamically. Each newly created row has a \"remove\" button.

Now if i clic

相关标签:
3条回答
  • 2020-12-31 03:21
     $(buttonSelector).live ('click', function ()
     {
        $(this).closest ('tr').remove ();
     }
     );
    

    using .live to bind your event will bind it automatically when your row is dynamically added.

    Edit

    live is now deprecated, since version 1.7 I think.

    The way to go now is using on instead of live.

    $('#tableid').on('click', buttonSelector, function(){
        $(this).closest ('tr').remove ();
    });
    

    See the doc.

    0 讨论(0)
  • 2020-12-31 03:36

    You could do something like:

    $('.add').click(function(){
    $("#tableid tbody:last").append('<tr><td>Hi</td><td><a class="remove">Remove</a>');
    });
    
    $('.remove').live('click',function(){console.log($(this).parent().parent().remove())});
    
    0 讨论(0)
  • 2020-12-31 03:38

    You can use this code to delete the parent row containing the clicked button:

    $(myButtonSelector).click(function(){
        $(this).parents('tr').first().remove();
    });
    

    For a live example see this link.

    For more information see this article.

    0 讨论(0)
提交回复
热议问题