【发布时间】:2016-03-15 07:45:57
【问题描述】:
我正在使用数据表。我在表中有一个操作列,其中有一个删除按钮,我正在使用 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);
};
});
}
});
})
我不知道如何解决这个问题。请帮助我摆脱这个问题。
【问题讨论】:
-
请发布您的渲染 HTML 而不是 PHP 代码
-
尝试:
$(document).on('click', ".delete_seminar", function(event) {...});如果您的按钮不在第一页上,则它不在 DOM 中,因此您需要将事件侦听器添加到文档中。
标签: javascript jquery html ajax datatables