范围滑块:jQuery会更改值,但滑块不会移动

时间:2020-01-18 10:57:35

标签: jquery slider

我运行以下程序:

val dm = DisplayMetrics()
getWindowManager().getDefaultDisplay().getMetrics(dm)

var screenAdjust:Float = (dm.densityDpi / 160f)

var x:Int = (116f * screenAdjust).toInt()
debugText.text=x.toString()
window.showAtLocation(changeTextSize, Gravity.CENTER_HORIZONTAL or Gravity.BOTTOM, 0, x)

运行程序后,滑块的“拇指”仍停留在99 输出(id = sliderVal;倒数第二行)显示:滑块值:3
我尝试过

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title> Slider </title>
    <!-- jQuery CDN https://code.jquery.com/ -->
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> 
    <script type="text/javascript"src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script>
    $(document).ready(function() {
        console.debug("jQuery "+ (jQuery ? $().jquery : "NOT") +" loaded"); // jQuery version to console
        $('#slider1').slider({value: "11"});                                // initialize slider
        $("#slider1").slider("option", "value", 2);       
        $("#slider1" ).slider("value", 3 );
        var value = $( "#slider1" ).slider( "option", "value");    // show slider value 
        $('#sliderVal').text("Slider Value: "+value);

        $('#slider1').on('input', function(event) {                     // show slider value when changed
            var valSlider1 =  $(this).attr('id') +"   Value:" +$(this).val();
            $('#sliderVal').text(valSlider1);
        });
    });
    </script>
</head>
<body>
    <p> Slider </p> 
    <input type="range" class="sl" id="slider1"  min="0" max="100" value="99">  
    <p id="sliderVal">...</p                     //display value of slider
</body>
</html>

它们都更改滑块的值,但是滑块的拇指永远不会移动。 当我用鼠标移动滑块的拇指时,将显示正确的值

1 个答案:

答案 0 :(得分:0)

只需像这样val()更改$("#slider1").val(2);

$(document).ready(function() {
  $("#slider1").val(11);
  var value = $("#slider1").val();
  $('#sliderVal').text("Slider Value: " + value);

  $('#slider1').on('input', function(event) { 
    var valSlider1 = $(this).attr('id') + "   Value:" + $(this).val();
    $('#sliderVal').text(valSlider1);
  });
});

$('#change').click(function() {
  $("#slider1").val(2);
  $('#sliderVal').text("Slider Value: 2");
});
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<p> Slider </p>
<input type="range" class="sl" id="slider1" min="0" max="100" value="99">
<p id="sliderVal">
</p>

<button id="change">Change</button>