[英]jQuery ONE function re-activates multiple times
I have many <div>
each containing another div that's my button. 我有很多
<div>
每个都包含另一个div,这是我的按钮。 I have an .one('click', '.button', function(){});
我有一个
.one('click', '.button', function(){});
that activates on each click and increments a number in a single div elsewhere. 会在每次点击时激活,并在其他位置的一个div中增加一个数字。
var barPrimC = 25;
var barPrimN = 6;
var barPrimS = 5;
var barPrimT = 48;
var barSecMajC = 11;
var barSecMajN = 2;
var barSecMajS = 3;
var barSecMajT = 19;
var incSL
$('.complete, .incomplete').click(function() {
$('.classInfo').slideUp();
if($(this).next().is(':hidden') == true) {
$(this).addClass('on');
$(this).next().show('slow');
incSL = this;
// ADD to List
$(".shortAdd").one('click','.button', function(){
$(this).text('Remove from Short List');
$(this).parent().attr("class", "shortRemove");
$(incSL).attr("class", "incompleteSL");
$('.classInfo').slideUp();
barPrimS++;
barSecMajS++;
var updateBarP = $(".bpS").find(".b-text-right").text(barPrimS);
var cat = $(this).data("category");
var catBar = $("."+cat).find(".bCommonS");
var updateBarS = catBar.filter(".bsS").find(".s-text-right").text(barSecMajS);
});
// REMOVE from List
$(".shortRemove").one('click','.button', function(){
$(this).text('Add to Short List');
$(this).parent().attr("class", "shortAdd");
$(incSL).attr("class", "incomplete");
$('.classInfo').slideUp();
barPrimS--;
barSecMajS--;
var updateBarP = $(".bpS").find(".b-text-right").text(barPrimS);
var cat = $(this).data("category");
var catBar = $("."+cat).find(".bCommonS");
var updateBarS = catBar.filter(".bsS").find(".s-text-right").text(barSecMajS);
});
}
});
I was originally using .on
but it was activating both of my methods (for ADD and REMOVE clicks) . 我最初使用
.on
但是它同时激活了我的两种方法(用于ADD和REMOVE点击) 。
If I have : 如果我有 :
<div>1st time div CLICKED (runs 1 time)</div>
<div>2nd time div CLICKED (runs 2 times)</div>
<div>3rd time div CLICKED (runs 3 times)</div>
<div>4th time div CLICKED (runs 4 times)</div>
So basically if barPrimS starts off at 5, it goes to 6 when the add
click method is ran. 因此,基本上,如果barPrimS从5开始,则在运行
add
click方法时将其变为6。 When a separate add
click method is ran, it goes to 8. Then to 11 then to 15. 当运行单独的
add
点击方法时,转到8。然后转到11,然后转到15。
I can't figure out why this is happening, please help! 我不知道为什么会这样,请帮忙! Should increment by only 1 (5, 6, 7, 8).
应该仅增加1(5、6、7、8)。 I've tried
barPrimS = barPrimS + 1
and other ways, but they all do the same. 我尝试了
barPrimS = barPrimS + 1
和其他方法,但是它们都一样。
I ended up removing the .shortAdd
and .shortRemove
.on('click')
methods from within the parent click even, as suggested, and didn't nest them. 我最终像建议的那样,从父点击中删除了
.shortAdd
和.shortRemove
.on('click')
方法,并且没有嵌套它们。 I then combined the short and add as an if/else
statement under the .shortAdd
and simply .toggleClass()
with the .shortRemove
class, checking to see if it existed. 然后,我组合short并在
.shortAdd
下添加为if/else
语句,并简单地将.toggleClass()
与.shortRemove
类一起检查是否存在。
This fixed all errors for me. 这为我解决了所有错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.