jQuery UI Slider在最低设置

时间:2016-06-30 08:07:10

标签: javascript jquery jquery-ui jquery-ui-slider

我有一个jQuery UI滑块的以下脚本。该脚本显示ID为#skizzar_payment_amount

的div中的金额

当我滚动到最低金额(0)时,div中的值显示初始金额而不是0。

见这里:

<script>
        jQuery(function($) {
            var initialValue = 7.5; 
            var maxValue = 20;
            var stepValue = 0.25;
            var sliderTooltip = function(event, ui) {
                var curValue = ui.value || initialValue; // current value (when sliding) or initial value (at start)
                var tooltip = '<div class="tooltip"><div class="tooltip-inner">£' + curValue + '</div><div class="tooltip-arrow"></div></div>';
                $('.ui-slider-handle').html(tooltip); //attach tooltip to the slider handle
                $("#skizzar_payment_amount").val(ui.value); //change amount in hidden input field


            }

            $("#slider").slider({
                value: initialValue,
                min: 0,
                max: maxValue,
                step: stepValue,
                create: sliderTooltip,
                slide: sliderTooltip,
            });
            $("#skizzar_payment_amount").val($("#slider").slider("value"));

        });
        </script>

https://jsfiddle.net/yzzvd52y/

3 个答案:

答案 0 :(得分:2)

ui.value将为0,此行中为falsevar curValue = ui.value || initialValue;

将其更改为例如:

var curValue = ui.value !== undefined ? ui.value : initialValue;

更新了fiddle

答案 1 :(得分:0)

我更新了您的fiddle并添加了以下代码:

change: function( event, ui ) {
             $("#skizzar_payment_amount").val(ui.value);
}

答案 2 :(得分:0)

试试这个:

jQuery(function($) {
  var initialValue = 7.5;
  var maxValue = 20;
  var stepValue = 0.25;
  var sliderTooltip = function(event, ui) {
    var curValue = ui.value >= 0 ? ui.value : initialValue; // current value (when sliding) or initial value (at start)
    var tooltip = '<div class="tooltip"><div class="tooltip-inner">£' + curValue + '</div><div class="tooltip-arrow"></div></div>';
    $('.ui-slider-handle').html(tooltip); //attach tooltip to the slider handle
    $("#skizzar_payment_amount").val(ui.value); //change amount in hidden input field


  }

  $("#slider").slider({
    value: initialValue,
    min: 0,
    max: maxValue,
    step: stepValue,
    create: sliderTooltip,
    slide: sliderTooltip,
    change: function(event, ui) {
      $("#skizzar_payment_amount").val(ui.value);
    }

  });
});