コードでデリゲートjqueryを使用していますが、イベントを複数回発生させると、 optionclickedクラスのイベントをbox-mainクラスにバインドしたためだとわかっていますが、私はしなければならない状況にありますoptionclickedクラスを使用するコンテンツは動的に生成されるため、これらのクラスを相互にバインドします。イベントが一度だけ発生すると、関数が1回呼び出され、ポップ操作が1回表示され、データが1回投稿されるなど、問題の解決策はありますか.
$('.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);
}
});
});