使用jquery动态删除表行;不起作用

Removing a table row dynamically using jquery doesn't work

本文关键字:不起作用 删除 jquery 动态 使用      更新时间:2024-05-26

此表是动态生成的。

<table class='myTab' id='selectPlaceTable' border='1px'>
    <tr>
    <td>Something</td>
    <td>Something</td>
    <td>Something</td>
    <td><input type='button' class='buttonRmv' id='buttonRmv' value='+'</td>
    </tr>
 </table>

jquery函数

$(document).ready(function(){
    $(".buttonRmv").on('click',function(){
            $(this).parent().parent().remove();
        });
});

使用此

检查演示

$(document).ready(function(){
    $(document.body).on('click',".buttonRmv",function(){
            $(this).parent().parent().remove();
    });
});

由于您正在动态生成HTML,请使用事件委派.closest(),如图所示:-

$(document).ready(function(){
    $(document.body).on('click',".buttonRmv",function(){
        $(this).closest('tr').remove();
    });
});

试试这个:

$(document).ready(function(){
    $('body').on('click',".buttonRmv",function(){
        $(this).closest('tr').remove();
    });
});

试试下面的代码:

$(document).ready(function(){
    $('body').on('click',".buttonRmv",function(){
        $(this).closest('tr').remove();
    });
});