php - Jquery 和 Bootstrap 4 dataTable 删除第 2 页上的行不起作用

标签 php jquery mysql sql modal-dialog

美好的一天!我有一个 jquery 和 bootstrap 4 数据表,每行都有分页和删除按钮,但在第 1 页充满数据后,它将转到第 2 页。问题是第 2 页中带有删除按钮行的数据不起作用,但在第 1 页中,删除按钮工作正常..

这是我的表代码:

<table class="table table-hover table-bordered" id="questionTable">
                                <thead>
                                    <tr>
                                        <th>No.</th>
                                        <th>Student</th>
                                        <th>Subject Code</th>
                                        <th>Semester</th>
                                        <th>SchoolYear</th>
                                        <th>Professor</th>
                                        <th></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php  while ($row = $result -> fetch_object()): ?>
                                    <tr>
                                        <td><?php echo  $row->Subject_Student_Taken;?></td>
                                        <td><?php echo $row-> Student_ID;?></td>
                                        <td><?php echo $row-> Subject_Code;?></td>
                                        <td><?php echo $row-> Term_Name;?></td>
                                        <td><?php echo $row-> SY_Name;?></td>
                                        <td><?php echo $row-> Faculty_ID;?></td>
                                        <td class="text-center">
                                            <a  class="btn btn-sm btn-outline-primary text-muted" href="SubjectTaken_edit.php?Subject_Student_Taken=<?php echo $row->Subject_Student_Taken;?>">Edit</a>

                                            <a  data-fid="<?php echo  $row->Subject_Student_Taken;?>" title="Delete Faculty" data-toggle="modal" data-target="#delete_modal"  class="btn btn-sm btn-outline-danger delete-faculty-btn text-muted" >Delete</a>
                                        </td>
                                    </tr>
                                    <?php endwhile; ?>

                                </tbody>
                            </table>

这是我的删除代码:

if(!empty($_POST['delete'])) { 
            $id = $_POST['delete']; 
            $data->delete_StudentTakenHandle($id, $conn); 

         }

函数代码:

 function delete_StudentTakenHandle($id, $conn){
        $sql = "DELETE FROM subject_taken_handle WHERE Subject_Student_Taken=? ";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("i", $id);
        $stmt->execute();    
    }

模态代码:

<div class="modal fade" id="delete_modal" role="dialog">
                      <div class="modal-dialog">
                        <div class="modal-content">
                          <!-- Modal Header -->
                          <div class="modal-header">
                            <h4 class="modal-title">Delete Confirmaiton</h4>
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                          </div>
                          <!-- Modal body -->
                          <div class="modal-body">
                            <div class="alert alert-danger" role="alert">
                              Are you sure you want to delete selected record?
                            </div>
                          </div>
                          <!-- Modal footer -->
                         <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">No</button>
                        <input type="hidden" name="delete"  id="row-id-to-delete">
                        <button type="submit" class="btn btn-danger" >Yes</button>
                      </div>
                        </div>
                      </div>
                    </div>

这是我的 jquery 代码:

<script>

        $('.delete-faculty-btn').on('click',function(){
         $("#row-id-to-delete").val($(this).data('fid'));

    });



</script>

最佳答案

该错误很可能是由以下原因引起的:

当您设置 jQuery 事件时,仅显示第一页中的元素(现有)。
由于 jQuery.on 函数仅将事件附加到现有元素,因此该事件不会附加到其他页面的元素。

解决方案:

您应该使用表的单击事件作为附加事件以删除按钮的包装器。

这样,每次单击并更改页面时,都会存在删除按钮,并且 jQuery.on 函数将能够将删除事件附加到它们。

您应该像这样包装事件附加代码:

$("#questionTable").on("click", ".delete-faculty-button", function(){
   // attach your delete event here
});

jQuery.on 的文档中了解更多信息.

关于php - Jquery 和 Bootstrap 4 dataTable 删除第 2 页上的行不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51751159/

相关文章:

javascript - 打开新窗口而不关注它

php - 通过另一个表过滤重复项

php - 需要基本了解 : PHP MySQLi Row Navigation

php - 将 php json 对象保存到 jquery 变量中

php 不返回 false 值

php - 根据ACF关系字段显示 "Related Posts"

javascript - jQuery e.which 在 switch 语句中

jquery - 如何在asp.net中将文本附加到Div表单jQuery?

mysql - 添加的联接越多,选择所需的时间就越长

php - 如何从 WordPress 中的帖子类型获取所有帖子