JQUERY 使用委托时事件触发不止一次 jquery
JQUERY event firing more than one time, when using delegate jquery
我在我的代码中使用委托 jquery,但是当它触发某个事件时它会触发不止一次,我知道这是因为我绑定了 optionclicked[=18 的事件=] class 到 boxes-main class 但我处于必须将这些 class 彼此绑定的情况,因为使用 optionclicked class 的内容是动态生成的。
有没有解决这个问题的方法,这样事件只触发一次意味着它调用一次函数并显示一次弹出操作和一次 post 数据等等。
$('.boxes-main').on('click', '.optionclicked', function(){
// do something
var timetoclick = parseFloat($('#time').text());
clearInterval(myCounter);
var optionclicked = $(this).attr('data');
var questionid = $(this).attr('data2');
$.post("quesanscheck.php", {
ans : optionclicked,
id : questionid,
time : timetoclick
}, function(data, status) {
alert(data);
if(data == optionclicked)
{
//alert("dfad");
setTimeout(function(){ rightans(); }, 1000);
}
else
{
// wrong ans red
if(optionclicked == 'A')
{
document.getElementById('op1').style.background = "red";
document.getElementById('op1').style.border = "0px";
}
if(optionclicked == 'B')
{
document.getElementById('op2').style.background = "red";
document.getElementById('op2').style.border = "0px";
}
if(optionclicked == 'C')
{
document.getElementById('op3').style.background = "red";
document.getElementById('op3').style.border = "0px";
}
if(optionclicked == 'D')
{
document.getElementById('op4').style.background = "red";
document.getElementById('op4').style.border = "0px";
}
// right ans green
if(data == 'A')
{
document.getElementById('op1').style.background = "green";
document.getElementById('op1').style.color = "white";
document.getElementById('op1').style.border = "0px";
}
if(data == 'B')
{
document.getElementById('op2').style.background = "green";
document.getElementById('op2').style.color = "white";
document.getElementById('op2').style.border = "0px";
}
if(data == 'C')
{
document.getElementById('op3').style.background = "green";
document.getElementById('op3').style.color = "white";
document.getElementById('op3').style.border = "0px";
}
if(data == 'D')
{
document.getElementById('op4').style.background = "green";
document.getElementById('op4').style.color = "white";
document.getElementById('op4').style.border = "0px";
}
setTimeout(function(){wrongans();}, 1000);
}
});
});
我遇到了类似的问题。
尝试将您的代码更改为:
$('.boxes-main').unbind('click').on('click', '.optionclicked', function(){
这将删除所有点击事件并添加一个事件。
当我对此进行调查时,点击事件触发了两次,即使点击事件只添加了一次。
我找不到对我有帮助的原始 SO post,但这对我有用。
我用一个名为 event
的变量绑定了点击事件并添加了代码
event.stopPropagation();
event.preventDefault();
这个解决了我的问题。
尝试使用:
event.stopPropagation();
它停止传播
我在我的代码中使用委托 jquery,但是当它触发某个事件时它会触发不止一次,我知道这是因为我绑定了 optionclicked[=18 的事件=] class 到 boxes-main class 但我处于必须将这些 class 彼此绑定的情况,因为使用 optionclicked class 的内容是动态生成的。 有没有解决这个问题的方法,这样事件只触发一次意味着它调用一次函数并显示一次弹出操作和一次 post 数据等等。
$('.boxes-main').on('click', '.optionclicked', function(){
// do something
var timetoclick = parseFloat($('#time').text());
clearInterval(myCounter);
var optionclicked = $(this).attr('data');
var questionid = $(this).attr('data2');
$.post("quesanscheck.php", {
ans : optionclicked,
id : questionid,
time : timetoclick
}, function(data, status) {
alert(data);
if(data == optionclicked)
{
//alert("dfad");
setTimeout(function(){ rightans(); }, 1000);
}
else
{
// wrong ans red
if(optionclicked == 'A')
{
document.getElementById('op1').style.background = "red";
document.getElementById('op1').style.border = "0px";
}
if(optionclicked == 'B')
{
document.getElementById('op2').style.background = "red";
document.getElementById('op2').style.border = "0px";
}
if(optionclicked == 'C')
{
document.getElementById('op3').style.background = "red";
document.getElementById('op3').style.border = "0px";
}
if(optionclicked == 'D')
{
document.getElementById('op4').style.background = "red";
document.getElementById('op4').style.border = "0px";
}
// right ans green
if(data == 'A')
{
document.getElementById('op1').style.background = "green";
document.getElementById('op1').style.color = "white";
document.getElementById('op1').style.border = "0px";
}
if(data == 'B')
{
document.getElementById('op2').style.background = "green";
document.getElementById('op2').style.color = "white";
document.getElementById('op2').style.border = "0px";
}
if(data == 'C')
{
document.getElementById('op3').style.background = "green";
document.getElementById('op3').style.color = "white";
document.getElementById('op3').style.border = "0px";
}
if(data == 'D')
{
document.getElementById('op4').style.background = "green";
document.getElementById('op4').style.color = "white";
document.getElementById('op4').style.border = "0px";
}
setTimeout(function(){wrongans();}, 1000);
}
});
});
我遇到了类似的问题。
尝试将您的代码更改为:
$('.boxes-main').unbind('click').on('click', '.optionclicked', function(){
这将删除所有点击事件并添加一个事件。
当我对此进行调查时,点击事件触发了两次,即使点击事件只添加了一次。
我找不到对我有帮助的原始 SO post,但这对我有用。
我用一个名为 event
的变量绑定了点击事件并添加了代码
event.stopPropagation();
event.preventDefault();
这个解决了我的问题。
尝试使用:
event.stopPropagation();
它停止传播