我有一个表并与数据表一起使用。在表中我使用了 Bootstrap 模式。模态在表格的第一页中工作正常,但在第一页之后模态不起作用。
我的表代码:
<table id="myTable" class="table table-striped">
<tr>
<td>
<a data-id="<?php echo $row['id']; ?>" class='userinfo'><i class="mdi mdi-account-edit"></i></a>
</td>
</tr>
</table>
我的数据表调用插件
$(".userinfo").on('click',function () {});
$(document).ready(function (){
var table = $('#myTable').dataTable({
dom: 'l<"toolbar">frtip',
initComplete: function(){
$("div.toolbar").html('<button type="button" data-toggle="modal" data-target="#add_staff"><span>Add New Staff</span><i style="margin-left: 2px;" class="fas fa-user-plus"></i></button>');
}
});
});
我的模态调用脚本
$(document).ready(function(){
$('.userinfo').on('click',function(){
var userid = $(this).data('id');
$.ajax({
url: 'inc/edit_staff_modal.php',
type: 'post',
data: {userid: userid},
success: function(response){
$('.mod').html(response);
$('#empModal').modal('show');
}
});
});
});
这是 bootstrap 的模态
<div class="modal fade" id="empModal" role="dialog">
<div class="modal-dialog modal-xl">
<div class="modal-content">
<div class="modal-body mod"></div>
</div>
</div>
</div>
在edit_staff_modal.php中,我简单地调用usedrid并获取数据。此代码在数据表的第 10 行数据中运行得非常好。但是,当我转到第二页和更多时,此模式不起作用,我搜索了几个博客和堆栈并遵循它们,但没有解决。我可以解决这个问题吗?
最佳答案
请更新以下代码以使用 jquery body 选择器绑定(bind)您的主要点击事件
$(document).ready(function(){
$("body").on("click", ".userinfo", function(event){
var userid = $(this).data('id');
$.ajax({
url: 'inc/edit_staff_modal.php',
type: 'post',
data: {userid: userid},
success: function(response){
$('.mod').html(response);
$('#empModal').modal('show');
}
});
});
});
希望这有效:)
关于javascript - Bootstrap Modal 在第二页的数据表中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59798068/