Javascript $POST方法多次触发
我在jQuery中有一个Javascript $POST方法多次触发,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,我在jQuery中有一个$POST方法,它将id作为数据发送到我的PHP文件,并使用该id从数据库中删除该行。我的问题是,每次单击delete按钮,都会多次调用POST方法 这是代码 $(".dash-wrapper .display-class").on('click', '.member-list-div .all-member-box .member-content-shell .single-row .action-column .action-holder .delete-image'
$POST
方法,它将id作为数据发送到我的PHP文件,并使用该id从数据库中删除该行。我的问题是,每次单击delete按钮,都会多次调用POST方法
这是代码
$(".dash-wrapper .display-class").on('click', '.member-list-div .all-member-box .member-content-shell .single-row .action-column .action-holder .delete-image', function() {
var div_id = $(this).closest('[id]').attr('id');
//$(".dash-wrapper .display-class .member-list-div .all-member-box .member-content-shell .modal .modal-content")
$(".dash-wrapper .display-class").on('click', '#yes1', function() {
$.post("delete.php", {
id1: div_id
}, function(data) {
if (data == 1) {
$('#myModal1').modal('hide');
$('body').removeClass('modal-open');
$('.modal-backdrop').remove();
ajaxcall3();
} else {
}
})
});
});
$(“.dash wrapper.display class”)。在('单击','上。成员列表div.all成员框。成员内容外壳。单行。操作列。操作保持器。删除图像',函数(){
});
//$(“.dash wrapper.display class.member list div.all member box.member content shell.modal.modal content”)
$(.dash wrapper.display class”)。在('click','yes1',函数(e)上{
var div_id=$(this).closest('[id]').attr('id');
$.post(“delete.php”{
id1:分区id
},函数(数据){
如果(数据==1){
$('#myModal1').modal('hide');
$('body').removeClass('modal-open');
$('.modal background').remove();
ajaxcall3();
}否则{
返回false;
}
}
)
e、 预防默认值();
});
在单击函数结束之前添加该行,并添加元素e
这将停止您的点击传播
$(“.dash wrapper.display class”)。在('单击','上。成员列表div.all成员框。成员内容外壳。单行。操作列。操作保持器。删除图像',函数(){
});
//$(“.dash wrapper.display class.member list div.all member box.member content shell.modal.modal content”)
$(.dash wrapper.display class”)。在('click','yes1',函数(e)上{
var div_id=$(this).closest('[id]').attr('id');
$.post(“delete.php”{
id1:分区id
},函数(数据){
如果(数据==1){
$('#myModal1').modal('hide');
$('body').removeClass('modal-open');
$('.modal background').remove();
ajaxcall3();
}否则{
返回false;
}
}
)
e、 预防默认值();
});
在单击函数结束之前添加该行,并添加元素e
这将停止您的点击传播
.one()
方法与.on()
方法相同,只是给定元素和事件类型的处理程序在第一次调用后解除绑定
例如:
$( "#foo" ).one( "click", function() {
alert( "This will be displayed only once." );
});
试一试
.one()
方法与.on()
方法相同,只是给定元素和事件类型的处理程序在第一次调用后解除绑定
例如:
$( "#foo" ).one( "click", function() {
alert( "This will be displayed only once." );
});
我猜每次你点击一个删除图像,你的AJAX调用就会被重复1次。这是因为每次单击删除图像时都会绑定一个新的事件处理程序,要修复它,可以在单击删除图像之前绑定一次单击
,并使用一个全局变量来获取id
,或者在每次重新分配时取消绑定单击处理程序,如下所示:
$(".dash-wrapper .display-class").on('click', '.member-list-div .all-member-box .member-content-shell .single-row .action-column .action-holder .delete-image', function() {
var div_id = $(this).closest('[id]').attr('id');
$(".dash-wrapper .display-class").off('click', '#yes1');
$(".dash-wrapper .display-class").on('click', '#yes1', function() {
$.post("delete.php", {
id1: div_id
}, function(data) {
if (data == 1) {
$('#myModal1').modal('hide');
$('body').removeClass('modal-open');
$('.modal-backdrop').remove();
ajaxcall3();
}
})
});
});
我猜每次你点击一个删除图像,你的AJAX调用就会被重复1次。这是因为每次单击删除图像时都会绑定一个新的事件处理程序,要修复它,可以在单击删除图像之前绑定一次单击
,并使用一个全局变量来获取id
,或者在每次重新分配时取消绑定单击处理程序,如下所示:
$(".dash-wrapper .display-class").on('click', '.member-list-div .all-member-box .member-content-shell .single-row .action-column .action-holder .delete-image', function() {
var div_id = $(this).closest('[id]').attr('id');
$(".dash-wrapper .display-class").off('click', '#yes1');
$(".dash-wrapper .display-class").on('click', '#yes1', function() {
$.post("delete.php", {
id1: div_id
}, function(data) {
if (data == 1) {
$('#myModal1').modal('hide');
$('body').removeClass('modal-open');
$('.modal-backdrop').remove();
ajaxcall3();
}
})
});
});
马上,你有两个点击这里。这可能就是问题所在吗
在ajaxcall3()中可能会创建某种循环吗
你不是那种每次只需要点击一次就点击两次的人,是吗
您的鼠标驱动程序或鼠标本身有问题吗
如果没有所有的代码,我只是想假设这是一件愚蠢的事情,所以请先尝试删除额外的点击
$(".dash-wrapper .display-class").on('click', '.member-list-div .all-member-box .member-content-shell .single-row .action-column .action-holder .delete-image', function(){
var div_id = $(this).closest('[id]').attr('id');
$.ajax({
type: "POST",
url: "delete.php",
data: "id1="+div_id,
dataType: "text",
async: true,
success: function(data) {
if (data == '1') {
$('#myModal1').modal('hide');
$('body').removeClass('modal-open');
$('.modal-backdrop').remove();
ajaxcall3();
}
}
});
})
$(“.dash wrapper.display class”)。在('单击','上。成员列表div.all成员框。成员内容外壳。单行。操作列。操作保持器。删除图像',函数(){
var div_id=$(this).closest('[id]').attr('id');
$.ajax({
类型:“POST”,
url:“delete.php”,
数据:“id1=“+div_id,
数据类型:“文本”,
async:true,
成功:功能(数据){
如果(数据='1'){
$('#myModal1').modal('hide');
$('body').removeClass('modal-open');
$('.modal background').remove();
ajaxcall3();
}
}
});
})
如果这还不能解决问题,请在发布之前尝试解除click事件的绑定
$(".dash-wrapper .display-class").on('click', '.member-list-div .all-member-box .member-content-shell .single-row .action-column .action-holder .delete-image', function(){
var div_id = $(this).closest('[id]').attr('id');
$.ajax({
type: "POST",
url: "delete.php",
data: "id1="+div_id,
dataType: "text",
async: true,
beforeSend: function() {
$('.member-list-div .all-member-box .member-content-shell .single-row .action-column .action-holder .delete-image').unbind('click');
},
success: function(data) {
if (data == '1') {
$('#myModal1').modal('hide');
$('body').removeClass('modal-open');
$('.modal-backdrop').remove();
ajaxcall3();
}
},
complete: function() {
$('.member-list-div .all-member-box .member-content-shell .single-row .action-column .action-holder .delete-image').bind('click');
}
});
});
$(“.dash wrapper.display class”)。在('单击','上。成员列表div.all成员框。成员内容外壳。单行。操作列。操作保持器。删除图像',函数(){
var div_id=$(this).closest('[id]').attr('id');
$.ajax({
类型:“POST”,
url:“delete.php”,
数据:“id1=“+div_id,
数据类型:“文本”,
async:true,
beforeSend:function(){
$('.member list div.all member box.member content shell.single row.action column.action holder.delete image')。解除绑定('click');
},
成功:功能(数据){
如果(数据='1'){
$('#myModal1').modal('hide');
$('body').removeClass('modal-open');
$('.modal background').remove();
ajaxcall3();
}
},
完成:函数(){
$('.member list div.all member box.member content shell.single row.action column.action holder.delete image').bind('click');
}
});
});
我没有测试这个,我只是边做边补。不保证它会工作
编辑
括号问题。一下子,你就有两个点击这里。这可能就是问题所在吗
在ajaxcall3()中可能会创建某种循环吗
你不是那种每次只需要点击一次就点击两次的人,是吗
您的鼠标驱动程序或鼠标本身有问题吗
如果没有所有的代码,我只是想假设这是一件愚蠢的事情,所以请先尝试删除额外的点击
$(".dash-wrapper .display-class").on('click', '.member-list-div .all-member-box .member-content-shell .single-row .action-column .action-holder .delete-image', function(){
var div_id = $(this).closest('[id]').attr('id');
$.ajax({
type: "POST",
url: "delete.php",
data: "id1="+div_id,
dataType: "text",
async: true,
success: function(data) {
if (data == '1') {
$('#myModal1').modal('hide');
$('body').removeClass('modal-open');
$('.modal-backdrop').remove();
ajaxcall3();
}
}
});
})
$(.dash wrapper.display class”)。在('cli