使用jQuery的HTML5范围滑块输入值

时间:2016-10-06 01:02:53

标签: jquery html5

我尝试做简单的if / else语句,例如“如果滑块上的值为100,则将带有一些文本的p标签附加到英里类”。我知道这很简单,但是我查看了jQuery文档并没有找到适合我的解决方案。

HTML:

<html><head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
    <div class="wrapper">
        <div class="miles"></div>
        <div class="battery-slider">
            <input class="slider-range" type="range" value="0" min="75" max="250" step="25">
            <span class="slider-value">0</span>
        </div>
    </div>
</body>
</html>

JavaScript的:

var rangeSlider = function(){
    var slider = $('.battery-slider'),
    range = $('.slider-range'),
    value = $('.slider-value');
    miles = $('.miles');

    slider.each(function(){
        value.each(function(){
            var value = $(this).prev().attr('value');
            $(this).html(value);
        });
        range.on('input', function(){
            $(this).next(value).html(this.value);
        });
    });

    if ( $(range).val(value) == 100 ) {
        $(miles).append( "<p>Pass</p>" );
    }
    else {
        $(miles).append( "<p>Fail</p>" );
    }
};
rangeSlider();

1 个答案:

答案 0 :(得分:2)

您的要求有点不清楚,但也许您正在寻找类似的东西:

&#13;
&#13;
var rangeSlider = function(){
    var slider = $('.battery-slider'),
    range = $('.slider-range'),
    value = $('.slider-value');
    miles = $('.miles');

    slider.each(function(){
      value.each(function(){
          var value = $(this).prev().attr('value');
          $(this).html(value);
      });
      range.on('input', function(){
        if ( this.value == 100 ) {
            $(miles).append( "<p>Pass</p>" );
        }
        else {
            $(miles).append( "<p>Fail</p>" );
        }

        $(this).next(value).html(this.value);
      });
    });
};
rangeSlider();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="wrapper">
    <div class="battery-slider">
        <input class="slider-range" type="range" value="0" min="75" max="250" step="25">
        <span class="slider-value">0</span>
    </div>
    <div class="miles"></div>
</div>
&#13;
&#13;
&#13;