【问题标题】:Rangeslider odd output value on only one numberRangeslider 仅在一个数字上的奇数输出值
【发布时间】:2023-04-05 20:48:02
【问题描述】:

我有一个“奇怪”的问题正在努力解决。

我正在使用 RangesliderJS (https://rangeslider.js.org),并且在大多数情况下一切正常。但是,我需要输出和实际字段值略有不同(由于其他计算方式,但我认为这在很大程度上无关紧要)。滑块显示百分比 - 从零到十。该值需要为 0.01 表示 1%,0.1 表示 10% 等等。

但是,对于用户,我需要显示“真实”数字 - 即 1%、5% 等。

我就是这样做的;

$(document).ready(function() {
    $('input[type="range"]').rangeslider({
polyfill : false,
onInit : function() {
    this.output = $( '<div class="range-output" />' ).insertAfter( this.$range ).html( this.$element.val() * 100 );
},
onSlide : function( position, value ) {
    this.output.html( value * 100 );
    console.log(this.value);
}
});
});

效果很好。但是,我有一个奇怪的场景,应该输出为 7% 的内容实际上显示为 7.000000000000001% - 每隔一个数字都显示为它应该显示的内容,并且控制台也将值正确记录为 0.07。

这纯粹是为了 UI 目的,所以如果有一个修复程序,我很乐意“破解”一个修复程序,但我也很想了解它为什么会这样做(JS 不是我的强项!)

【问题讨论】:

    标签: javascript rangeslider


    【解决方案1】:

    虽然我对原因仍然一无所知,但我已经通过使用 Math.round 解决了这个问题;

          $('input[type="range"]').rangeslider({
    polyfill : false,
    onInit : function() {
        this.output = $( '<div class="range-output" />' ).insertAfter( this.$range ).html( this.$element.val() * 100 );
    },
    onSlide : function( position, value ) {
        this.output.html(Math.round( value * 100 ));
        console.log(this.value);
    }
    });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-16
      • 2014-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多