javascript - 数据表删除按钮在除第一个页面之外的其他页面上不起作用

标签 javascript jquery html ajax datatables

我正在使用数据表。我在表中有一个操作列,其中有一个删除按钮,我正在使用 ajax post 函数删除任何行。但删除按钮仅在第一页有效。它在其他页面上不起作用。 这是我的删除按钮。

<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
    <tr>   
        <th>#</th>
        <th class="col-md-5">Resource Person</th>
        <th class="col-md-4">Title/Topic</th>
        <th> Date </th>
        <th>Actions</th>
    </tr>
</thead>
<tbody>
    <?php 
    $sql = "SELECT * FROM seminar";
    $result = $con->query($sql);

    if ($result->num_rows > 0) {
                                    // output data of each row
        $count = 1;
        while($row = $result->fetch_assoc()) { ?>
        <tr>
            <td><?= $count ?></td>
            <td><?= $row['person'] ?></td>
            <td><?= $row['topic'] ?></td>
            <td><?= $row['date'] ?></td>
            <td>
                <button class="btn btn-danger delete_seminar" value="<?php echo $row['id'] ?>"> <span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button>
                <button class="btn btn-info" onclick="location.href = 'addRecord.php?id=<?php echo $row['id'] ?>';"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span></button>
            </td>
        </tr>
        <?php $count++; }

    }else{
        echo "<tr><td colspan='5'><center>No Record Found</center></td></tr>"; 
    }
    ?>

</tbody>

还有我的 jquery 函数。

$('#example').DataTable();    
$(function() {
        $(".delete_seminar").on('click', function(event) {

            if (confirm("Are you sure?")) {
                var data = $(this).val();
                $.post('requests/seminars.php', {delete_sem: data}, function(data) {
                    if (data == "delete") {
                        location.reload();
                    }else{
                        alert(data);
                    };

                });
            }

        });    
    })

我不知道如何解决这个问题。请帮助我解决这个问题。

最佳答案

您必须像下面这样编辑您的 JavaScript

$(document).on('click', '.delete_seminar', function(e){..});

关于javascript - 数据表删除按钮在除第一个页面之外的其他页面上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36005240/

相关文章:

php - 从 php 中的数组构造 javascript

jquery - 外部js文件jquery函数似乎没有获取clientId

html - 链接 css 文件的替代方法?

javascript - 将多个对象作为 1d 传递到数组中

javascript - set array = 多维对象数组的一维值

javascript - 在 ReactJS 中渲染数据

javascript - 广播问答 Activity

javascript - 如果图像正常,为什么和img onerror处理程序一起执行

html - CSS 向右浮动奇怪的行为

javascript - API 端点资源上没有 Access-Control-Allow-Origin header