简体   繁体   English

如何在jQuery滑块旋钮上显示当前滑块值?

[英]How to display the current slider value on the jQuery slider knob?

I'm trying to figure out from the jQuery example here whether it's possible to set the current value of the slider as text that appears on the slider knob. 我试图从这里的jQuery示例中找出是否可以将滑块的当前值设置为出现在滑块旋钮上的文本。

So, as the user changes the slider, the new value would continually update as a number value displayed as text on the surface of the slider knob in real time. 因此,随着用户更换滑块,新值将不断更新为实时在滑块旋钮表面上显示为文本的数字值。

  $(document).ready(function() {
    $("#slider").slider();
  });

  <div id="slider"></div>

Is there a way to do that? 有没有办法做到这一点?

@EvilMM's answer is almost right but s/he forgot the quotes and I've also experienced the slider value being -1 so I wrote in a fix for that. @EvilMM的答案几乎是正确的,但他/他忘记了引号,而且我也遇到过滑块值为-1因此我为此做了一个修正。

<div id="slider"></div>

js: JS:

$("#slider").slider({
    max: 10,
    slide: function (event, ui) {
        m_val = ui.value;
        if (m_val < 0) {
            m_val = 0;
            $(this).slider({ value: 0 });
        }
        $(this).find("a:first").text(m_val);
    }
});

If you are using Jquery UI 如果您正在使用Jquery UI

$(function(){
var slider = $('#slider1').bxSlider({      
  onBeforeSlide: function(currentSlide, totalSlides){
       alert(currentSlide);  
  }      
});

Done! 完成!

Lets say your slider is called "slider", just like in you example: 可以说您的滑块称为“滑块”,就像您的示例一样:

<div id="slider" class="slider"></div>

Then, inside this div, there is a generated link-tag (a) that represents the knob. 然后,在该div内,将生成一个表示旋钮的链接标签(a)。 So you could try something like that: 因此,您可以尝试执行以下操作:

$(document).ready(function() {
  $("#slider").slider(
    slide: function(event, ui){
      $("#slider").find(a:first).text(ui.value);
    }
  );
});

This should write the current value on the knob while sliding. 滑动时应将当前值写在旋钮上。 What you now have to do, is to play around with the css to format the knob. 您现在要做的是使用CSS来设置旋钮的格式。

I hope this will help a little bit. 我希望这会有所帮助。

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

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