简体   繁体   English

如何获得 Bootstrap 滑块的最小值和最大值?

[英]How do i get Min and Max value of Bootstrap slider?

I have the following slider in my razor我的剃须刀中有以下滑块

<input id="rbSlider" type="text" class="span2" value="" data-slider-min="0" data-slider-max="1000" data-slider-step="5" data-slider-value="[0,1000]" />

On Click, i need to get its current Min and Max values.单击时,我需要获取其当前的最小值和最大值。

i tried using following without luck,我尝试在没有运气的情况下使用以下方法,

    var min = $('#rbSlider').data('slider').min;
    var max = $('#rbSlider').data('slider').max;

Its returning not defined error, How do i get min and max?它返回未定义的错误,我如何获得最小值和最大值?

You almost had it.你几乎拥有它。 The attributes data-slider-min="0" data-slider-max="1000" can be accessed like this:属性data-slider-min="0" data-slider-max="1000"可以这样访问:

var min = $('#rbSlider').data('sliderMin');
var max = $('#rbSlider').data('sliderMax');

Note that I have removed the - and used CamelCase.请注意,我已经删除了-并使用了 CamelCase。 Take a look at the API and you will see that jQuery takes $( "div" ).data( "lastValue" ) === 43;看一看API ,你会看到 jQuery 采用$( "div" ).data( "lastValue" ) === 43; :

... searches among the attributes of the element, converting a camel-cased string into a dashed string and then prepending data- to the result. ... 在元素的属性中搜索,将驼峰式字符串转换为虚线字符串,然后将 data- 添加到结果中。 So, the string lastValue is converted to data-last-value.因此,字符串 lastValue 被转换为 data-last-value。

So for consistency, use camel casing (just watch out for this )因此,为了保持一致性,请使用驼色外壳(请注意这一点

You were so close.你是如此接近。 Try this.尝试这个。

var min = $('#rbSlider').data('slider-min');
var max = $('#rbSlider').data('slider-max');

Oh finally i got it working using the following哦,最后我使用以下方法让它工作了

var min = $('#rbSlider').data('slider').options.value[0];
var max = $('#rbSlider').data('slider').options.value[1];

Improvement for dynamically set limits动态设置限制的改进

@jcuenod's answer will retrieve the min/max values as set in the initial HTML's DOM. @jcuenod 的答案将检索初始 HTML DOM 中设置的最小值/最大值。 However, if you are changing the limits later via JavaScript (ie $("#rbSlider").slider('option', {min: 5, max: 100}) ), this will not get you the up-to-date values但是,如果您稍后通过 JavaScript 更改限制(即$("#rbSlider").slider('option', {min: 5, max: 100}) ),这不会让您获得最新的价值观

Instead, you need to use $("#rbSlider").data('slider').options.max相反,您需要使用$("#rbSlider").data('slider').options.max

var min = $('#rbSlider').slider('option','min');
var max = $('#rbSlider').slider('option','max');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM