【发布时间】:2019-04-27 03:58:55
【问题描述】:
问题出在我已经成功编写了在选中复选框上添加和删除行的代码,但它仅适用于第一个元素。
就像当我单击第一个元素时,它会添加一行并在取消选中它时第二次删除该行,当我选中并取消选中第二个复选框时,它会在选中时添加一行并在未选中时删除行。
这里是jquery代码
<script type="text/javascript">
$(document).ready(
function () {
$('.finding').change(
function () {
if ($('.finding').is(':checked')) {
var finding = $(this).next('label').text();
$('#findings-table').append("<tr><td>"+finding+"</td></tr>");
}
else {
$('#findings-table').empty();
}
});
});
</script>
这是我得到复选框的 php 代码
if ($checkResult > 0) {
for ($i=0; $i < $checkResult ; $i++) {
$result = mysqli_fetch_assoc($query);
$findings = $result['name'];
$count = 1 + $i;
echo"<tr>
<th scope='row'>$count</th>
<td scope='col'>
<div class='input-group'>
<input class='finding form-check-input' type='checkbox' name='findings[]' value='$findings'>
<label class='input-label pt-1 px-3'>$findings</label>
</div>
</td>
</tr>";
}
}
最后是我的 html 代码,其中出现了复选框
<div class='table-container' style="height: 21vh;">
<table class="table table-hover" style="line-height: 7px;">
<tbody>
<?php include 'getfindings.php'; ?>
</tbody>
</table>
</div>
【问题讨论】: