如何在“slidechange”中更改jquery滑块的值事件而不触发另一个“幻灯片更改”事件
我已经陷入这个循环有一段时间了。基本上我有一个滑块,当滑块值发生变化时执行一些操作。我做的第一件事是进行检查,如果检查未通过,我需要重置滑块的值。但是重置滑块的值会改变它!所以我被困住了。这是我的代码:
Slider:
$(element).slider({
min: 0,
max: 10,
step: 1,
value: initialValue,
animate: false,
slide: _updateOnSlide,
change: _performAction
});
var _performAction = function(evt, ui){
if (CT.travel.atLeastOneStepPresent()){
var id = $(this).attr("id");
_updateOnSlide(evt, ui);
if (id != "travelTime"){//se è il tempo non aggiorno
CT.makeQuery();
}
CT.timeline.update();
}else{
alert("you can't do this");
$(this).slider("value", 0);
_updateOnSlide(evt, ui);
}
}
问题是 $(this).slider("value", 0);
触发另一个更改事件! 有人知道如何解决这个问题吗?
我需要使用更改功能,因为我通过单击按钮而不是滑动来更改滑块的值。
编辑 - evt.originalEvent 对我来说总是未定义的(如果我像 console.log(evt.originalEvent) 一样 cosole.log ),但出于某种奇怪的原因,如果我 console.dir 它,我会看到它。但不幸的是它不能解决问题,我在这里添加了改变滑块值的代码部分(请原谅我之前忘记这样做了)
$('.plus').click(function(event){
var sliderCurrentValue = $( this ).next().slider( "value" );
$(this).next().slider( "value", sliderCurrentValue + 1 );
});
如您所见,该值第一次以编程方式更改,所以无论如何,对 evt.originalEvent 的检查是没有用的。
编辑2 - 我找到了解决这个问题的方法,但我希望有更好的方法:
alert("you can't do this");
//destroy the slider
$(this).slider("destroy");
// recreate the slider
_makeSelector(this);
我认为如果我只是删除绑定到“更改”事件的函数并重置该值,它也可以工作,但我希望有更好的方法来做到这一点!
i've been stuck in this loop for a while. Basically i have a slider that performs some action when the slider values changes. First thing i do is make a check, and if that check doesn't pass, i need to reset the value of the slider. But resetting the value of the slider changes it!And so i'm stuck. Here is my code:
Slider:
$(element).slider({
min: 0,
max: 10,
step: 1,
value: initialValue,
animate: false,
slide: _updateOnSlide,
change: _performAction
});
var _performAction = function(evt, ui){
if (CT.travel.atLeastOneStepPresent()){
var id = $(this).attr("id");
_updateOnSlide(evt, ui);
if (id != "travelTime"){//se è il tempo non aggiorno
CT.makeQuery();
}
CT.timeline.update();
}else{
alert("you can't do this");
$(this).slider("value", 0);
_updateOnSlide(evt, ui);
}
}
The problem is that $(this).slider("value", 0);
triggers another change event!
Anyone knows how to solve this?
I need to use the change function because i'm changing the values of the slider by clicking buttons, not by sliding it.
EDIT - evt.originalEvent is always undefined for me (if i cosole.log it like this console.log(evt.originalEvent)) but for some strange reason i see it if i console.dir it. But unfortunately it doesn't solve the problem, i add here the part of my code that vhanges the value of the slider (pardon me i forgot to do that earlier)
$('.plus').click(function(event){
var sliderCurrentValue = $( this ).next().slider( "value" );
$(this).next().slider( "value", sliderCurrentValue + 1 );
});
As you can see, the value is changed programmatically the first time and so the check on the evt.originalEvent would be useless anyway.
EDIT 2 - i've found a way out of this but i hope there is a better way:
alert("you can't do this");
//destroy the slider
$(this).slider("destroy");
// recreate the slider
_makeSelector(this);
I think it could also work if i just removed the function binded to the "change" event and reset the value, but i hope there is a better way of doing this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查
evt.originalEvent
是否存在。如果存在,则表明更改是由用户使用自己的控件发起的。如果不是,则表明程序启动了更改。
编辑 - 最简单的解决方案可能是在重置滑块之前临时
.unbind()
change
处理程序,然后.bind
之后立即再次执行。Check for the existence of
evt.originalEvent
.If it's there, the change was initiated by the user using its own controls. If it's not, it indicates a program initiated change.
EDIT - probably the easiest solution is to temporarily
.unbind()
thechange
handler before you reset the slider, and then.bind
it again immediately afterwards.