问题描述
我正在使用 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!');
}
});
这篇关于引导滑块更改值处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!