最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do i get Min and Max value of Bootstrap slider? - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Jun 30, 2015 at 13:46 Burak Ozdemir 5,3323 gold badges25 silver badges30 bronze badges asked Jun 24, 2015 at 6:34 Nithin PaulNithin Paul 2,1993 gold badges36 silver badges62 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

You almost had it. The attributes data-slider-min="0" data-slider-max="1000" can be accessed like this:

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

Note that I have removed the - and used CamelCase. Take a look at the API and you will see that jQuery takes $( "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. So, the string lastValue is converted to 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. However, if you are changing the limits later via JavaScript (i.e. $("#rbSlider").slider('option', {min: 5, max: 100})), this will not get you the up-to-date values

Instead, you need to use $("#rbSlider").data('slider').options.max

var min = $('#rbSlider').slider('option','min');
var max = $('#rbSlider').slider('option','max');
发布评论

评论列表(0)

  1. 暂无评论