Bootstrap滑块更改值处理程序 [英] Bootstrap Slider change value handler
问题描述
我正在使用Bootstrap Slider,我想知道是否有任何方法可以包含用于值更改的处理程序.我看到文档中包含了slideStart,slide和slideStop的处理程序,但没有值"更改"的处理程序.
I'm using Bootstrap Slider, and I was wondering if there is any way to include a handler for a value change. I see that the documentation includes handlers for slideStart, slide and slideStop, but none for "change" (of value).
他们可能会滑动滑块,并(在释放时)最终获得相同的值,在这种情况下,我不希望事件触发.仅当他们将其滑动到新值时.
It's possible that they could slide the slider around and (upon release) end up on the same value, in which case I would NOT want the event to fire. Only if they slide it to a new value.
我尝试过:
$('#sliderAdminType').slider({
formater: function(value) {
// my formater stuff that works
},
change: function(event, ui) {
console.log("has changed");
}
});
和
$('#sliderAdminType').change(function() {
console.log("has changed");
});
和
$('#sliderAdminType').slider().change(function() {
console.log("has changed");
});
和
$('body').on('change', '#sliderAdminType', function(e){
console.log("has changed");
});
推荐答案
您可以执行以下步骤:
1)使用slideStart
事件获取滑块的原始值
1) Use slideStart
event to get the original value of your slider
2)使用slideStop
事件获取滑块的新值
2) Use slideStop
event to get the new value of your slider
3)比较这两个值,如果不同,则执行代码
3) Compare this two value, if it's different then fire your code
var originalVal;
$('.span2').slider().on('slideStart', function(ev){
originalVal = $('.span2').data('slider').getValue();
});
$('.span2').slider().on('slideStop', function(ev){
var newVal = $('.span2').data('slider').getValue();
if(originalVal != newVal) {
alert('Value Changed!');
}
});
Fiddle Demo
这篇关于Bootstrap滑块更改值处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!