Py学习  »  Jquery

无法使用jQuery.remove()删除html表行

Drake Perera • 4 年前 • 766 次点击  

我正试图使用jqueryty删除一行表。这是我的html代码。

$('.delete_btn').click(function(){
                var key = $(this).attr("key");
                $('client'+key).remove();
            });  
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="client_table">
        <tbody>
            <tr class="client1">
                <td>1</td>
                <td>Content</td>
                <td><input class="delete_btn" type="button" key="1" value="Delete"></td>
            </tr>
        </tbody>
    </table>

我在里面用这个代码 $(文档).ready() . 但不幸的是,代码不起作用。我已经简化了代码,只想从表和DOM中删除行。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/52561
 
766 次点击  
文章 [ 5 ]  |  最新文章 4 年前
Hitesh Tripathi
Reply   •   1 楼
Hitesh Tripathi    4 年前

添加 . 在类名之前

$('.delete_btn').click(function(){
                var key = $(this).attr("key");
                $('.client'+key).remove();
            });  
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="client_table">
        <tbody>
            <tr class="client1">
                <td>1</td>
                <td>Content</td>
                <td><input class="delete_btn" type="button" key="1" value="Delete"></td>
            </tr>
        </tbody>
    </table>
Ahmed Gaafer
Reply   •   2 楼
Ahmed Gaafer    4 年前

您缺少类选择器 . 我加了它,效果很好

$('.delete_btn').click(function(){
  var key = $(this).attr("key");
  $('.client'+key).remove();
});  
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="client_table">
    <tbody>
        <tr class="client1">
            <td>1</td>
            <td>Content</td>
            <td><input class="delete_btn" type="button" key="1" value="Delete"></td>
        </tr>
    </tbody>
</table>
Anonymous
Reply   •   3 楼
Anonymous    4 年前

你可以这样试试

$('.delete_btn').click(function(){
            var key = $(this).attr("key");
            $('.client'+ key).remove();
});  
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="client_table">
    <tbody>
        <tr class="client1">
            <td>1</td>
            <td>Content</td>
            <td><input class="delete_btn" type="button" key="1" value="Delete"></td>
        </tr>
    </tbody>
</table> 
chuu
Reply   •   4 楼
chuu    4 年前

尝试使用选择器。

您可以使用最近或父选择器到达tr

$('.delete_btn').click(function(){
            $(this).closest('tr').remove(); // or .parent()
        }); 
Gokul Maha
Reply   •   5 楼
Gokul Maha    4 年前

您应该在Jquery选择器中的类名之前添加“.” $('.client'+密钥)

$('.delete_btn').click(function(){
        var key = $(this).attr("key");
        $('.client'+key).remove();
});