Javascript datatables jquery单击事件分页后不工作
我正在使用 我试图运行一个onclick事件,该事件在第一页正常工作,但一旦我转到第2页(或任何其他页),它就停止工作Javascript datatables jquery单击事件分页后不工作,javascript,jquery,ajax,datatable,Javascript,Jquery,Ajax,Datatable,我正在使用 我试图运行一个onclick事件,该事件在第一页正常工作,但一旦我转到第2页(或任何其他页),它就停止工作 我使用jquery-1.10.2.min.js和1.9.4版本的datatable,因为事件仅附加到现有元素 您应该将其更改为: $("#tableId").on("click", ".activeAccount", function(){ // your code goes here }); 在的文档中阅读更多信息。我也有同样的问题。每次我的AJAX函数(modalC
我使用jquery-1.10.2.min.js和1.9.4版本的datatable,因为事件仅附加到现有元素 您应该将其更改为:
$("#tableId").on("click", ".activeAccount", function(){
// your code goes here
});
在的文档中阅读更多信息。我也有同样的问题。每次我的AJAX函数(modalContentAjaxRefresh)更新表时,分页停止。因此,我不得不将代码从: 发件人: $('.modal toggle').off('click',modalContentAjaxRefresh').on('click', ModalContentajax(刷新) 致: $(“#DataTables_Table_0_wrapper”)。在(“单击“,”。模式切换“, ModalContentajax(刷新) 我在datatable中的按钮是: 模式切换 数据样式=“放大” href=“/setting/account/{{account\u turn.uuid}}/update” 数据切换=“模态”数据目标=“#editAccount”wecare method=“GET”>
正如@squaleLis所说,事件只附加到现有元素 因此,在我的例子中,我为按钮定义了onclick事件并调用它
<button class='btn btn-success activeAccount' onclick="YourMethod();">Activate Account</button>
function YourMethod() {
....same code..
}
激活帐户
函数方法(){
…相同的代码。。
}
最重要的是在单击分页时重新分配元素
//function to assign event
var assign_actions = function(){
//Some code
}
//when the page is ready
$( document ).ready(function() {
assign_actions();
//Code to assign event when paginate
$('.paginate_button').on('click', function(){
assign_actions();
});
});
product list是加载数据并启用分页的表
这对我来说非常有效。表内容中是否有
.activeAccount
按钮?另外,请注意,使用async:false
是非常糟糕的做法,成功处理程序中的location.reload()
完全否定了首先发出AJAX请求的全部意义。是的,它位于表的内容下。在这种情况下,您需要使用委托事件处理程序,正如@squaleLis的回答。但是,您确实应该去掉async:false
和location.reload()
。我在这里给出了答案。您可能会看到,@Md.HarunOrRashid建议的解决方案非常简单helpful@sarower-jahan谢谢:)谢谢,这是非常有用的答案。在我的例子中,在点击事件之前是“$”(“.each_player”)。点击(function(){//my code goes here});“修改后,”$(“#playerGroupUI”)。点击(“click”,“.each_player”,function(){//my code goes here});”这会多次而不是一次触发事件!?10删除页面上的图标,单击其中一个图标后,删除确认被触发两次
<button class='btn btn-success activeAccount' onclick="YourMethod();">Activate Account</button>
function YourMethod() {
....same code..
}
//function to assign event
var assign_actions = function(){
//Some code
}
//when the page is ready
$( document ).ready(function() {
assign_actions();
//Code to assign event when paginate
$('.paginate_button').on('click', function(){
assign_actions();
});
});
$(document).on("click",".activeAccount",function(e){
// your code goes here
});
$("#product-list").on("click",".btn-delete-product",function(e){
e.preventDefault();
var prodId = $(this).attr("product-id");
.... code to delete the record from the db...
});