Jquery UI Slider - Input a Value and Slider Move to Location

こ雲淡風輕ζ 提交于 2019-12-21 05:36:10

问题


I was wondering if anyone has found a solution or example to actually populating the input box of a slider and having it slide to the appropriate position onBlur() .. Currently, as we all know, it just updates this value with the position you are at. So in some regards, I am trying to reverse the functionality of this amazing slider.

One link I found: http://www.webdeveloper.com/forum/archive/index.php/t-177578.html is a bit outdated, but looks like they made an attempt. However, the links to the results do not exist. I am hoping that there may be a solution out there.

I know Filament has re-engineered the slider to handle select (drop down) values, and it works flawlessly.. So the goal would be to do the same, but with an input text box.


回答1:


Will this do what you want?

$("#slider-text-box").blur(function() {
  $("#slider").slider('option', 'value', parseInt($(this).val()));
});

Each option on the slider has a setter as well as a getter, so you can set the value with that, as in the example above. From the documentation:

//getter
var value = $('.selector').slider('option', 'value');
//setter
$('.selector').slider('option', 'value', 37);

UPDATE:

For dual sliders you'll need to use:

$("#amount").blur(function () {
  $("#slider-range").slider("values", 0, parseInt($(this).val()));
});
$("#amount2").blur(function () {
  $("#slider-range").slider("values", 1, parseInt($(this).val()));
});

You'll need to use Math.min/max to make sure that one value doesn't pass the other, as the setter doesn't seem to prevent this.

You were almost there when you were using the $("#slider-range").slider("values", 0) to get each value. A lot of jQuery has that kind of get/set convention in which the extra parameter is used to set the value.




回答2:


I've done some work around the jQuery UI slider to make it accept values from a textbox, it may not be exactly what you were after but could help:

http://chowamigo.blogspot.com/2009/10/jquery-ui-slider-that-uses-text-box-for.html




回答3:


$slider = $("#slider");
            $("#amountMin").blur(function () {      
              $slider.slider("values", 0,Math.min($slider.slider("values", 1),parseInt($(this).val()) ) );
              $(this).val(Math.min($slider.slider("values", 1),parseInt($(this).val())));

            });
            $("#amountMax").blur(function () {
              $slider.slider("values",1,Math.max($slider.slider("values", 0),parseInt($(this).val()) ) );                              
              $(this).val(Math.max($slider.slider("values", 0),parseInt($(this).val())));
            });

I just used martin's code and updated the id to #slider also added the math.max as he suggested so the sliders won't overlap.



来源:https://stackoverflow.com/questions/1330008/jquery-ui-slider-input-a-value-and-slider-move-to-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!