twitter-bootstrap 如何获取滑块引导程序的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15736694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the value of the slider bootstrap?
提问by NETTO
How to get the values of the slider bootstrap to hidden iputs?
如何将滑块引导程序的值获取到隐藏的 iputs?
<input type="hidden" name="min_value" id="min_value" value="">
<input type="hidden" name="max_value" id="max_value" value="">
$(function() {
$( "#slider-range-s1" ).slider({
range: true,
min: 0,
max: 500,
value: [ 0, 500 ]
});
});
回答by ilovett
I think it's currently broken.
我认为它目前已损坏。
Documentation states:
文档说明:
Methods
.slider('getValue')
Get the value.
方法
.slider('getValue')
获取价值。
How ever, calling $('#sliderDivId').slider('getValue')returns the jQuery selector rather than the value...
然而,调用$('#sliderDivId').slider('getValue')返回的是 jQuery 选择器而不是值......
For now you could manually grab it from the data object...
$('#sliderDivId').data('slider').getValue()
现在你可以手动从数据对象中抓取它......
$('#sliderDivId').data('slider').getValue()
回答by vadimchin
fast fix:
快速修复:
$.fn.slider = function (option, val) {
if (typeof option == 'string') {
if (this.length) {
var data = this.eq(0).data('slider');
if (data)
return data[option](val);
return null;
}
}
return this.each(function () {
var $this = $(this),
data = $this.data('slider'),
options = typeof option === 'object' && option;
if (!data) {
$this.data('slider', (data = new Slider(this, $.extend({}, $.fn.slider.defaults, options))));
}
})
};
回答by jules
The problem is, that there is more than 1 element with the same ID (the slider divand the inputinside the slider div), in this example $('#sliderDivId'). If you select the input element with JQuery, you can use the slider API.
问题是,不存在具有相同ID(滑块多于1种元素的div和输入滑块内的div),在该示例$(“#sliderDivId”)。如果使用 JQuery 选择输入元素,则可以使用滑块 API。
$('input[id="sliderDivId"]').slider('getValue')
回答by Dominic Williams
At time of writing it's a bit flaky but you can make it work:
在撰写本文时,它有点不稳定,但您可以使其工作:
1/ Make sure you specify an empty range starting "value[0]". Unless you do this its internal value field becomes undefined e.g.
1/ 确保您指定了一个以“value[0]”开头的空范围。除非您这样做,否则其内部值字段将变为未定义,例如
$('#setBidDialogSlider').slider({ value: [0], max: max, step: 0.2, formater: function(v) { return v.toFixed(2)+"BTC" } });
2/ Now finally you can catch the value in the event
2/ 现在你终于可以捕捉到事件中的价值了
$('#setBidDialogSlider').slider().on('slide', function(ev) {
$scope.dialog.order_value = ev.value*$scope.dialog.price;
});
回答by Joseph Dailey
I know I'm late but this is still broken but to fix it open up bootstrap-sliders.jsand inject these three things:
我知道我迟到了,但这仍然坏了,但要修复它,请打开bootstrap-sliders.js并注入以下三样东西:
the very end of the first function: Slider:
第一个函数的最后Slider:
this.calculateValue();
right before:return val;in the calculateValuefunction
就在之前:return val;在calculateValue函数中
grandVal = val;
after the $.fn.sliderfunction:
后$.fn.slider功能:
$.fn.sliderValue = function(){
return grandVal;
};
var grandVal;
Now you can access the value by $('#MySliderId').sliderValue()
现在您可以通过以下方式访问该值 $('#MySliderId').sliderValue()
回答by Alaa M. Jaddou
you can use this from the built in function
您可以从内置函数中使用它
$( "#slider-range-s1" ).slider({
range: true,
min: 0,
max: 500,
value: [ 0, 500 ]
slide: function( event, ui ) {
// u could use this function
$(".min-slider-value").html( "$" + ui.values[ 0 ]);
$(".max-slider-value").html( "$" + ui.values[ 1 ]);
},
change: function(event, ui) {
// or u could use this function
$(".min-slider-value").html( "$" + ui.values[ 0 ]);
$(".max-slider-value").html( "$" + ui.values[ 1 ]);
}
});
and thank you i thought i coud share this with you guys :D
谢谢你,我想我可以和你们分享这个:D

