HTML Range Slider and jQuery?

这一生的挚爱 提交于 2019-12-08 23:59:27

问题


I have the code below in my website and integrated a range slider using HTML5. I want the slider to update the text-input with the value and vice versa. Would I do this using jQuery? If so can you give me example code?

Thanks.

<input type="text" name="chance" id="chance" class="text" value="50">

<input type="range" id="chance" class="vHorizon" min="0.01" max="98" style="background-color: #00aec8; width: 50%;">

回答1:


You can use change event on slider and keyup event on textbox to update the values.

HTML

<input type="text" name="chance" id="chance" class="text" value="50">

<input type="range" id="chanceSlider" class="vHorizon" min="0.01" max="98" step="0.01" style="background-color: #00aec8; width: 50%;">

jQuery

$('#chanceSlider').on('change', function(){
    $('#chance').val($('#chanceSlider').val());
});

$('#chance').on('keyup', function(){
    $('#chanceSlider').val($('#chance').val());
});

And jsfiddle to play with

http://jsfiddle.net/tDkEW/1/




回答2:


If you want to refresh the value in real time use mousemove, or you can just use change but it's not at real time.

$('yourselector').on('mousemove change',function() { 
    //your code
});



回答3:


An improvement I found useful

$('#chanceSlider').on('input change', function(){ $('#chance').val($('#chanceSlider').val()); });

jsfiddle http://jsfiddle.net/tDkEW/408/




回答4:


You could definitely use jQuery.

$('input#chance').on('change', function() { 
    $('input[name=chance]').val($(this).val()); 
});

$('input[name=chance]').on('change keyup', function() {
    $('input#chance').val($(this).val());
});



回答5:


Yes, you can do it with html5 elements and jQuery, like this:

http://jsfiddle.net/sRbHZ/

HTML:

<input type="text" name="chance" id="chance" class="text" value="50">
<input type="range" id="chance_slider" class="vHorizon" min="0.01" max="98" style="background-color: #00aec8; width: 50%;">

JS:

var slider = $('#chance_slider'),
    textbox = $('#chance');

slider.change(function() {
    var newValue = parseInt($(this).val());

    textbox.val(newValue);
});

textbox.change(function() {
    var newValue = parseInt($(this).val());

    slider.val(newValue);
});

EDIT

To allow decimals:

var slider = $('#chance_slider'),
    textbox = $('#chance');

slider.change(function() {
    var newValue = $(this).val();

    textbox.val(newValue);
});

textbox.change(function() {
    var newValue = $(this).val();

    slider.val(newValue);
});



回答6:


First Ids must be unique.You can use jQuery slider().You can use text box here to update that value in slider and vice versa




回答7:


I suggest caching the jquery selectors to improve performance.
Also ids must be unique, see fiddle.

var $chance_txt = $('#chance_txt'),
    $chance_slider = $('#chance_slider').on('change', function() {
        $chance_txt.val($chance_slider.val());
    });

jsfiddle




回答8:


Make your ids unique first.

HTML

<input type="text" name="chance" id="display" class="text" value="50">
<input type="range" id="slider" class="vHorizon" min="0.01" max="98" style="background-color: #00aec8; width: 50%;">

JQuery

$('#slider').on('change',function(){
  $('#display').val($('#slider').val());
});


来源:https://stackoverflow.com/questions/17482636/html-range-slider-and-jquery

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