繁体   English   中英

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

[英]Jquery and Bootstrap 4 dataTable delete row on page 2 not working

美好的一天! 我有一个带有分页的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函数仅将事件附加到现有元素,因此该事件未附加到其他页面的元素。

解:

您应该将表的click事件用作包装器,以将事件附加到删除按钮。

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

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

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

jQuery.on文档中阅读更多内容

使用on的委托格式,而不是click .. on()| jQuery API文档

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

});

变为:

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

        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM