数据表阻止除第 1 页之外的其他页面上的按钮单击事件
我有一个数据表,其中包含我网站中的所有类别。有一个功能可以使用 AJAX 删除类别,同时选择一个类别将产品(在我的例子中是网站)移动到另一个类别。我是这样做的:
$(".delete_category_instant").on('click', function(event) {
$(this).hide();
$(this).closest("td").find(".editCategory").hide();
$(this).closest("td").find(".selectCategoryToMove").removeClass("d-none");
});
$(".selectCategoryToMove").find(".submitSelection").on('click', function(event){
$(this).closest("td").find(".selectCategoryToMove").addClass("d-none");
$(this).closest("td").find(".confirmAction").removeClass("d-none");
Toast.fire({
icon: 'warning',
title: 'Please confirm your action!'
});
});
$(".confirmActionYes").on('click', function(event) {
var cat_id = $(this).attr('category-id');
var move_cat_id = $(this).closest("td").find("select").val();
$this = $(this);
$.ajax({
type: 'GET',
url: '{{ url("administrator/delete/category/") }}',
data: {
id: cat_id,
move_id: move_cat_id
},
success: function(response) {
if(response.status == 1) {
Toast.fire({
icon: 'success',
title: 'Category deleted successfully!'
});
$this.parents('tr').remove();
} else if(response.status == 2) {
Toast.fire({
icon: 'warning',
title: 'You are not allowed to do this action!'
});
} else {
Toast.fire({
icon: 'error',
title: 'Could not delete this category!<br/>Please try again later!'
});
}
},
error: function(xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});
});
$(".confirmActionNo").on('click', function(event) {
$(this).parent().addClass("d-none");
$(this).parent().closest("td").find(".delete_category_instant").show();
$(this).parent().closest("td").find(".editCategory").show();
});
但这仅适用于数据表中的第 1 页。在其他页面内,该按钮甚至在单击时没有响应。那有什么办法解决呢?这是我将来需要注意的常见问题吗?
I have a datatable with all the categories in my website. There is a function to delete a categori with AJAX while selecting a category to move the products(websites in my case) to another category. Here's how I do it:
$(".delete_category_instant").on('click', function(event) {
$(this).hide();
$(this).closest("td").find(".editCategory").hide();
$(this).closest("td").find(".selectCategoryToMove").removeClass("d-none");
});
$(".selectCategoryToMove").find(".submitSelection").on('click', function(event){
$(this).closest("td").find(".selectCategoryToMove").addClass("d-none");
$(this).closest("td").find(".confirmAction").removeClass("d-none");
Toast.fire({
icon: 'warning',
title: 'Please confirm your action!'
});
});
$(".confirmActionYes").on('click', function(event) {
var cat_id = $(this).attr('category-id');
var move_cat_id = $(this).closest("td").find("select").val();
$this = $(this);
$.ajax({
type: 'GET',
url: '{{ url("administrator/delete/category/") }}',
data: {
id: cat_id,
move_id: move_cat_id
},
success: function(response) {
if(response.status == 1) {
Toast.fire({
icon: 'success',
title: 'Category deleted successfully!'
});
$this.parents('tr').remove();
} else if(response.status == 2) {
Toast.fire({
icon: 'warning',
title: 'You are not allowed to do this action!'
});
} else {
Toast.fire({
icon: 'error',
title: 'Could not delete this category!<br/>Please try again later!'
});
}
},
error: function(xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});
});
$(".confirmActionNo").on('click', function(event) {
$(this).parent().addClass("d-none");
$(this).parent().closest("td").find(".delete_category_instant").show();
$(this).parent().closest("td").find(".editCategory").show();
});
But that's working only on page 1 in the datatable. Inside other pages the button isn't even responding on click. What's the solution of that? Is that a common problem that I need to be aware of in the future?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的事件处理程序可能未连接到新元素,因为设置它们的代码仅在页面加载时运行一次。从你的例子中无法看出,但如果是这种情况,你可以使用委托监听器来代替:
例如:
变成
It's possible that your event handlers are not connecting to new elements because the code that sets them up only runs once, on page load. Can't tell from your example, but if that's the case you can use delegated listeners instead:
For example:
becomes