
本文旨在解决在动态生成的HTML表格中,为每一行添加独立的Accept按钮功能时遇到的问题。通过修改JavaScript代码,利用jQuery选择器准确定位每一行中的元素,并使用CSS类名替代重复的ID,确保Accept按钮的点击事件只影响当前行,从而实现预期的交互效果。
在动态生成的HTML表格中,为每一行添加一个Accept按钮,点击后显示特定列并隐藏其他列,是一个常见的需求。然而,如果处理不当,可能会出现点击一个按钮影响所有行的问题。本文将详细介绍如何正确实现这一功能,并避免常见的错误。
原始代码的问题在于使用了相同的ID(showOptions和refuseAccept)多次。在HTML中,ID应该是唯一的,因此document.getElementById只会返回第一个匹配的元素。这导致点击任何一行的Accept按钮,都只会影响表格的第一行。
解决此问题的关键在于:
以下是修改后的代码示例:
HTML (PHP)
<tbody>
<?php
$sql = "SELECT * FROM appointments INNER JOIN patients ON appointments.patientID =patients.patientID WHERE docID='$doctorId'";
$stmt = $conn->prepare($sql);
$stmt->execute();
$i=0;
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$i++;
extract($row);
echo"<tr>
<td >$i</td>
<td>{$patientFName} {$patientLName}</td>
<td>{$AppStart}</td>
<td>{$AppEnd}</td>
<td class='refuseAccept'>
<button type='button' class='btn btn-outline-danger'>refuse</button>
<button type='button' class='btn btn-outline-success m-2 acceptPpomentDoc' >accept</button>
</td>
<td class='showOptions m-2' style='display:none;'>
<a href='#' title='view Details' class='text-success p-2 addappoment' > <i class='fas fa-calendar-check'></i></a>
<a href='#' title='Edit' class='text-primary p-2 editBtn' ><i class='fas fa-user-edit'></i> </a>
<a href='#' title='Delete' class='text-danger p2 deleteBtn' ><i class='fas fa-user-times'></i> </a>
</td>
</tr>";
}
?>
</tbody>注意以下几点修改:
JavaScript (jQuery)
$(document).on('click', '.acceptPpomentDoc', function() {
$(this).closest('tr').find('.showOptions').show();
$(this).closest('tr').find('.refuseAccept').hide();
});这段代码的关键在于:
CSS (可选)
为了确保 showOptions 默认是隐藏的,可以在CSS中添加以下样式:
.showOptions {
display: none;
}或者直接在HTML中通过行内样式设置。
以下是一个完整的示例,包括HTML、JavaScript和CSS:
<!DOCTYPE html>
<html>
<head>
<title>Accept Button Example</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
.showOptions {
display: none;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Start</th>
<th>End</th>
<th>Actions</th>
<th>Options</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>John Doe</td>
<td>9:00</td>
<td>10:00</td>
<td class='refuseAccept'>
<button type='button' class='btn btn-outline-danger'>refuse</button>
<button type='button' class='btn btn-outline-success m-2 acceptPpomentDoc'>accept</button>
</td>
<td class='showOptions m-2'>
<strong>ACCEPTED</strong>
<a href='#' title='view Details' class='text-success p-2 addappoment'> <i class='fas fa-calendar-check'></i></a>
<a href='#' title='Edit' class='text-primary p-2 editBtn'><i class='fas fa-user-edit'></i> </a>
<a href='#' title='Delete' class='text-danger p2 deleteBtn'><i class='fas fa-user-times'></i> </a>
</td>
</tr>
<tr>
<td>2</td>
<td>Jane Smith</td>
<td>10:00</td>
<td>11:00</td>
<td class='refuseAccept'>
<button type='button' class='btn btn-outline-danger'>refuse</button>
<button type='button' class='btn btn-outline-success m-2 acceptPpomentDoc'>accept</button>
</td>
<td class='showOptions m-2'>
<strong>ACCEPTED</strong>
<a href='#' title='view Details' class='text-success p-2 addappoment'> <i class='fas fa-calendar-check'></i></a>
<a href='#' title='Edit' class='text-primary p-2 editBtn'><i class='fas fa-user-edit'></i> </a>
<a href='#' title='Delete' class='text-danger p2 deleteBtn'><i class='fas fa-user-times'></i> </a>
</td>
</tr>
</tbody>
</table>
<script>
$(document).on('click', '.acceptPpomentDoc', function() {
$(this).closest('tr').find('.showOptions').show();
$(this).closest('tr').find('.refuseAccept').hide();
});
</script>
</body>
</html>通过使用类名代替ID,并利用jQuery选择器准确定位每一行中的元素,可以有效地解决在动态生成的HTML表格中,为每一行添加独立的Accept按钮功能时遇到的问题。这种方法不仅可以避免ID重复的问题,还可以提高代码的可维护性和可扩展性。
以上就是为表格的每一行创建独立的Accept按钮功能的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号