gpt4 book ai didi

javascript - 如何在表的多个 tbody 中使用 jquery 删除选定的 tbody

转载 作者:行者123 更新时间:2023-11-30 15:19:19 25 4
gpt4 key购买 nike

我有一个表,并使用 jquery 在我的表中添加了更多 tbody。现在我想在单击删除按钮时删除选定的 tbody,我的代码删除一行而不是删除所有选定的 tbody。怎么办?我的代码是:

               <div class='row' id="table_container">
<div class='col-xs-12 col-sm-12 col-md-12 col-lg-12'>
<table class="table table-bordered table-hover" id="invoice_table">
<thead>

<tr>
<th> <input id="check_all" class="formcontrol" type="checkbox"/></th>
<td>User Information</td>
</tr>

</thead>
<tbody>
<tr>
<td><span class="ui-icon ui-icon-arrowthick-2-n-s"></span> <input class="case" type="checkbox"/></td>
<td>Info</td>
</tr>
<tr>
<td> Name</td>
<td><input type="text" name="name[]" id="name_1" class="form-controlt"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lastname[]" id="lastname_1" class="form-control"></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class='col-xs-12 col-sm-3 col-md-3 col-lg-3'>
<button class="btn btn-danger delete" type="button">- Delete</button>
<button class="btn btn-success addmore" type="button">+ Add More</button>
</div>

<script type="text/javascript">

var i=$('table tbody').length+1;
$(".addmore").on('click',function(){
html = '<tbody>';
html = '<tr>';
html += '<td> <span class="ui-icon ui-icon-arrowthick-2-n-s"></span> <input class="case" type="checkbox"/></td>';
html += '<td>Info</td>';
html += '</tr>';
html += '<tr>';
html += '<td>Name</td>';
html += '<td><input type="text" name="name[]" id="name_'+i+'" class="form-control"></td>';
html += '</tr>';
html += '<tr>';
html += '<td>Last Name</td>';
html += '<td><input type="text" name="lastname[]" id="lastname_'+i+'" class="form-control"></td>';
html += '</tr>';
html += '</tbody>';
$('table').append(html);
i++;
});

//to check all checkboxes
$(document).on('change','#check_all',function(){
$('input[class=case]:checkbox').prop("checked", $(this).is(':checked'));
});


$(".delete").on('click', function(){
$('.case:checkbox:checked').parents("tr").remove();
$('#check_all').prop("checked", false);
});


</script>

最佳答案

您的问题源于这行代码:

$('.case:checkbox:checked').parents("tr").remove();

.parents("tr")选择复选框的祖先 <tr>元素。尝试:

$(".case:checkbox:checked").closest("tbody").remove();

关于javascript - 如何在表的多个 tbody 中使用 jquery 删除选定的 tbody,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43994624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com