javascript - 表格中只有第一行按钮起作用

标签 javascript php html

问题:在完整列中,单击后有"is"按钮,显示已咨询,但问题是只有一行正在工作。我不明白我如何唯一地定义每一行。请看一下这段代码,让我知道我在这里犯了什么错误。

<div class="card-body">
    <div class="table-responsive ">
<?php
    $query = "SELECT * FROM book_now";
    $query_run = mysqli_query($conn, $query);
?>
        <table class="table table-bordered table table-striped" id="dataTable" cellspacing="0">
            <thead class="table table-success text-center" style="width: 80%">
                <tr>
                    <th style="width: 1%"> S.No </th>
                    <th> Patient Name </th>
                    <th> UHID </th>
                    <th style="width: 12%"> Time Slot </th>
                    <th> Contact No </th>
                    <th> Complete </th>
                    <th> Write Prescription </th>
                </tr>
            </thead>
            <tbody>
<?php
if(mysqli_num_rows($query_run) > 0)        
{
    foreach($query_run as $row)
    {
?>
                <tr class="text-center">
                    <td><?php  echo $row['booking_id']; ?></td>
                    <td><?php  echo $row['name']; ?></td>
                    <td><?php  echo $row['booked_by']; ?></td>
                    <td><?php  echo $row['appointment_time_slot']; ?></td>
                    <td><?php  echo $row['phone']; ?></td>
                    <td>
                        <form action="" method="post">
                            <input onclick="func()" id="accountDetails" type="button" value="Yes"></input>
<script>
function func() {
    document.getElementById('accountDetails').value  = 'Consulted';
}
</script>
                        </form>

最佳答案

问题是因为您正在使用 id重复内容中的属性会创建重复项。 id必须是独一无二的。

要解决此问题,请更改 idclass ,然后您可以创建一个事件处理程序来监听任何 button 上的任何点击元素。从那里,您可以使用传递给事件处理程序的 Event 对象来仅更新被单击的按钮。

请注意,在下面的示例中,使用了不显眼的事件处理程序而不是内联 onclick属性。后者不是好的做法,应该避免。另请注意,该事件被委托(delegate)给父级 table 。这允许我们使用单个事件处理程序,无论表包含多少行/按钮,或者它们是在 DOM 加载后动态创建的。

document.querySelector('#dataTable').addEventListener('click', e => {
  if (e.target.matches('.accountDetails'))  
    e.target.value = 'Consulted';
});
<div class="card-body">
  <div class="table-responsive ">
    <table class="table table-bordered table table-striped" id="dataTable" cellspacing="0">
      <thead class="table table-success text-center" style="width: 80%">
        <tr
          <th> Complete </th>
          <th> Write Prescription </th>
        </tr>
      </thead>
      <tbody>
        <tr class="text-center">
          <td>
            <form action="" method="post">
              <input class="accountDetails" type="button" value="Yes" />
            </form>
          </td>
        </tr>
        <tr class="text-center">
          <td>
            <form action="" method="post">
              <input class="accountDetails" type="button" value="Yes" />
            </form>
          </td>
        </tr>        
        <tr class="text-center">
          <td>
            <form action="" method="post">
              <input class="accountDetails" type="button" value="Yes" />
            </form>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

关于javascript - 表格中只有第一行按钮起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74358086/

相关文章:

php - 使用 array_map() 访问一级键而不调用 `array_keys()`

html - IE9和Chrome之间的兼容性(带有链接的边框图像)

python - 仅抓取 <p> 而不嵌入 <a>

html - 如何制作像 youtube 一样的叠加层

javascript - 悬停时自动播放 html 视频?

javascript - 根据 <select> 值重定向页面

php - Laravel belongsToMany 仅返回特定列

php - 使用 laravel dusk 进行登录测试

javascript - 为什么这个 JS 在 ie8 中不工作?

javascript - UTC 日期字符串是否需要格式说明符 "Z",即使该字符串包含时间偏移?