简体   繁体   English

多个div的jQuery范围滑块值框

[英]jQuery range slider value box for multiple divs

I'm new in js and jQuery and I managed to create a simple slider with a value indicator box under it, that displays the slider's current value. 我是js和jQuery的新手,我设法创建了一个简单的滑块,下面有一个值指示框,显示滑块的当前值。 I have used the following code: 我使用了以下代码:

HTML HTML

<div class="Slider">
    <input type="range" id="slider" min="0" max="100">
</div> 
<b><div id="Value">value: %</div></b> 

JS JS

    $(window).on("load", function(){
         var slider = $("#slider");
         slider.change(function(){
             $("#Value").html (" value : " + this.value + "%"); 
        });
    });  

I want to use the same slider in multiple div that are hidden and appear in the same position when i click some links. 我想在隐藏的多个div中使用相同的滑块,当我点击某些链接时,它会出现在相同的位置。 The problem is that although the slider and the value box appear, the output value which is controlled by the jQuery function won't work but for the first div. 问题是虽然出现了滑块和值框,但是由jQuery函数控制的输出值将不起作用,但对于第一个div。

Can anybody tell me what to change to fix this? 谁能告诉我要改变什么来解决这个问题?

The problem is that you're having multiple sliders with the same ID: id="slider" and divs with the same ID: id="Value" . 问题是你有多个具有相同ID的滑块: id="slider"和具有相同ID的div: id="Value" Use class instead of id: 使用class而不是id:

<div class="Slider">
    <input type="range" class="slider-input" min="0" max="100">
    <b><div class="Value">value: %</div></b>
</div>

And then use event delegation like this: 然后像这样使用事件委托:

$('.Slider').on('change', '.slider-input', function(){
    $(this).parent().find('.Value').html (" value : " + this.value + "%"); 
});

Of course that if you set a style in your CSS to that element, then you need to replace all the instances of #slider to .slider-input . 当然,如果您将CSS中的样式设置为该元素,则需要将#slider所有实例#slider.slider-input

A working example: 一个工作的例子:

 $(function() { $('.Slider').on('change', '.slider-input', function(){ $(this).parent().find('.Value').html (" value : " + this.value + "%"); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="Slider"> <input type="range" class="slider-input" min="0" max="100"> <b><div class="Value">value: %</div></b> </div> <div class="Slider"> <input type="range" class="slider-input" min="0" max="100"> <b><div class="Value">value: %</div></b> </div> <div class="Slider"> <input type="range" class="slider-input" min="0" max="100"> <b><div class="Value">value: %</div></b> </div> 

Using class sliderInputRange it possible to attach one event-handler to multiple input-elements. 使用类sliderInputRange可以将一个事件处理程序附加到多个input-elements。 Attribute sliderInputRange allows to specify the div which receives the value: 属性sliderInputRange允许指定接收值的div:

<div class="Slider"><input type="range" class="sliderInputRange" min="0" 
   max="100" data-value-id="value1"></div> 
<b><div id="value1">value: %</div></b> 

<div class="Slider"><input type="range" class="sliderInputRange" min="0" 
   max="100" data-value-id="value2"></div> 
<b><div id="value2">value: %</div></b> 

With .attr() the id of the value-element is retrieved: 使用.attr()检索value-element的id:

$(window).on("load", function(){
     var slider = $(".sliderInputRange");
     slider.change(function(){

        var value_id= $(this).attr("data-value-id");

         $("#" + value_id).html (" value : " + this.value + "%"); 
    });
});  

Please try for youreself: https://embed.plnkr.co/peXfdt/ 请试试你自己: https ://embed.plnkr.co/peXfdt/

Dynamic add slider. 动态添加滑块。

 var sliderComponent = '<div class="slider_container">' + '<input type="range" class="slider" min="0" max="100">' + '<br>' + 'value: <span class="value"></span>%' + '</div>'; var addSlider = function() { var currentSlider = $(sliderComponent); $(".sliders").append(currentSlider); currentSlider.on("change", ".slider", function() { $(this).siblings(".value").html( $(this).val() ); }); }; $(document).ready(function() { $("button").on("click", addSlider) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="add_slider">ADD</button> <div class="sliders"> </div> 

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

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