我在其他的下面有几个小的PHP表:
蓝色是一张桌子,依此类推

我希望在单击带有加号的绿色按钮时能够向表中添加新行。但是如何检测with table下的按钮是否被点击,以便在该表下添加新的行呢?
我的表的代码如下:
<?php
$result = mysqli_query($conn, "SELECT category_name FROM categories"); // using mysqli_query instead
while($res = mysqli_fetch_array($result)) {
echo "<table id='tbl' class='col-12'><th name='category' class='open-list bg-grey txt-xl pl-3'>".$res['category_name']."</th>
<tr class='content-div row align-items-center'>
<td class='col-8'><input name='product_name' type='text' class='p-3 border-0 text-left txt-xl'></td>
<td class='col-2 p-0 txt-xl'><input name='quantity' type='text' value='1' class='w-50 border-0 p-0 txt-xl'><label class='txt-xl m-0 text-left'>tk</label></td>
<td class='col-2' ><input class='mr-3' type='checkbox' name='check'></td>
</tr>
<tr>
<td>
<button type='button' class='add_another bg-success m-2 col-2 additem-btn shadow d-flex justify-content-center'><i class='txt-lg-icon bi bi-plus'></i></button>
</td>
</tr>";
}
?>
</table>到目前为止,我尝试了以下方法:
$('document').ready(function() { $('.add_another').click(function() {
$("#tbl").append('<tr><td><input name="product_name" type="text" class="pr-3 text-left txt-xl col-12 border-dark"></td><td><input class="mx-3" type="checkbox" name="check"></td></tr>');});})它可以添加新行,但无论我在哪个表下单击add按钮,它总是只在第一个表中添加新行。
表头(灰色背景的表头)来自我的mysql数据库,每个表头也有一个id。因此,也许有一种方法可以使用id-s来检测按钮被单击的位置,或者是否有其他方法来解决我的问题?
编辑:
我还想通过单击表头在打开和关闭表头下的内容之间切换。
到目前为止,我已经尝试了这些:
$(".open-list" ).click(function() {$( ".content-div" ).toggle("");});
$(function() {
$('.open-list').on("click",function() {
$(".content-div").closest("tr").toggle("")
});
})它们一起打开和关闭所有表内容,但我希望能够单独打开/关闭每个表内容。
谢谢!
发布于 2021-03-10 18:33:58
ID必须是唯一的
如果要在按钮后添加行,请尝试执行以下操作
$(this).closest("tr").after()
或
$(this).closest("tbody").append()
如果在带有按钮的行后使用
$(function() {
$('.add_another').on("click",function() {
$(this).closest("tr").before('<tr><td><input name="product_name" type="text" class="pr-3 text-left txt-xl col-12 border-dark"></td><td><input class="mx-3" type="checkbox" name="check"></td></tr>');
});
})<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class='col-12'>
<th name='category' class='open-list bg-grey txt-xl pl-3'>Whatever</th>
<tr class='row align-items-center'>
<td class='col-8'><input name='product_name' type='text' class='p-3 border-0 text-left txt-xl'></td>
<td class='col-2 p-0 txt-xl'><input name='quantity' type='text' value='1' class='w-50 border-0 p-0 txt-xl'><label class='txt-xl m-0 text-left'>tk</label></td>
<td class='col-2'><input class='mr-3' type='checkbox' name='check'></td>
</tr>
<tr>
<td>
<button type='button' class='add_another bg-success m-2 col-2 additem-btn shadow d-flex justify-content-center'><i class='txt-lg-icon bi bi-plus'></i>Add</button>
</td>
</tr>
</table>
<table class='col-12'>
<th name='category' class='open-list bg-grey txt-xl pl-3'>Whatever</th>
<tr class='row align-items-center'>
<td class='col-8'><input name='product_name' type='text' class='p-3 border-0 text-left txt-xl'></td>
<td class='col-2 p-0 txt-xl'><input name='quantity' type='text' value='1' class='w-50 border-0 p-0 txt-xl'><label class='txt-xl m-0 text-left'>tk</label></td>
<td class='col-2'><input class='mr-3' type='checkbox' name='check'></td>
</tr>
<tr>
<td>
<button type='button' class='add_another bg-success m-2 col-2 additem-btn shadow d-flex justify-content-center'><i class='txt-lg-icon bi bi-plus'></i>Add</button>
</td>
</tr>
</table>
发布于 2021-03-10 18:50:46
您可以使用this运算符并查找table的父行来添加新行,而不是使用id,如下所示。
$('document').ready(function() {
$('.add_another').click(function() {
$(this).parents('table').append('<tr><td><input name="product_name" type="text" class="pr-3 text-left txt-xl col-12 border-dark"></td><td><input class="mx-3" type="checkbox" name="check"></td></tr>');
});
})<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<table id='tbl' class='col-12'>
<th name='category' class='open-list bg-grey txt-xl pl-3'>one</th>
<tr class='row align-items-center'>
<td class='col-8'><input name='product_name' type='text' class='p-3 border-0 text-left txt-xl'></td>
<td class='col-2 p-0 txt-xl'><input name='quantity' type='text' value='1' class='w-50 border-0 p-0 txt-xl'><label class='txt-xl m-0 text-left'>tk</label></td>
<td class='col-2'><input class='mr-3' type='checkbox' name='check'></td>
</tr>
<tr>
<td>
<button type='button' class='add_another bg-success m-2 col-2 additem-btn shadow d-flex justify-content-center'><i class='txt-lg-icon bi bi-plus'></i>Add</button>
</td>
</tr>
</table>
<table id='tb2' class='col-12'>
<th name='category' class='open-list bg-grey txt-xl pl-3'>one</th>
<tr class='row align-items-center'>
<td class='col-8'><input name='product_name' type='text' class='p-3 border-0 text-left txt-xl'></td>
<td class='col-2 p-0 txt-xl'><input name='quantity' type='text' value='1' class='w-50 border-0 p-0 txt-xl'><label class='txt-xl m-0 text-left'>tk</label></td>
<td class='col-2'><input class='mr-3' type='checkbox' name='check'></td>
</tr>
<tr>
<td>
<button type='button' class='add_another bg-success m-2 col-2 additem-btn shadow d-flex justify-content-center'><i class='txt-lg-icon bi bi-plus'></i>Add</button>
</td>
</tr>
</table>
https://stackoverflow.com/questions/66562755
复制相似问题