Javascript 如何使用jquery在表的多个tbody中删除选定的tbody
我有一个表,使用jquery在表中添加了更多的tbody。现在,我想在单击“删除”按钮时删除选定的tbody。我的代码删除一行,而不是删除所有选定的tbody。你怎么能做到?我的代码是:Javascript 如何使用jquery在表的多个tbody中删除选定的tbody,javascript,jquery,html,html-table,Javascript,Jquery,Html,Html Table,我有一个表,使用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
<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>
用户信息
信息
名称
姓
-删除
+添加更多
变量i=$('table tbody')。长度+1;
$(“.addmore”)。在('click',function()上{
html='';
html='';
html+='';
html+='Info';
html+='';
html+='';
html+='Name';
html+='';
html+='';
html+='';
html+=‘姓氏’;
html+='';
html+='';
html+='';
$('table').append(html);
i++;
});
//选中所有复选框
$(文档)。在('change','check#u all',函数()上{
$('input[class=case]:checkbox').prop(“checked”,$(this).is(':checked'));
});
$(“.delete”)。在('click',function()上{
$('.case:checkbox:checked').parents(“tr”).remove();
$('check#u all').prop(“checked”,false);
});
您的问题源于以下代码行:
$('.case:checkbox:checked').parents("tr").remove();
.parents(“tr”)
选择复选框的祖先,这些祖先是
元素。尝试:
$(".case:checkbox:checked").closest("tbody").remove();
谢谢,但没有使用您的代码删除所有tbody我只想删除选定的tbody。这是因为您实际上没有添加多个
tbody
。此代码html='';html=''代码>应具有+=“”