jQuery UI滑块最大值不正确 [英] Jquery UI Slider incorrect max value

查看:46
本文介绍了jQuery UI滑块最大值不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在初始化一个jQuery UI滑块,例如,其值是.01,最小值是.01,最大值是5000.我的问题是最大值在4999.990000000001处停止.可以看到我在做什么错吗?

I am initializing a jquery ui slider that has for example a value of .01, a min of .01, and a max of 5000. My issue is that the max is stopping at 4999.990000000001. Can you see what I am doing wrong?

$( "#sliderModal" ).slider({
    value:r.min_price/100,
    min: r.min_price/100,
    max: r.max_price/100,
    step:0.01,
    slide: function( event, ui ) {
       $( "#amountModal" ).val(ui.value );
    }
});

推荐答案

这是与float值相关的舍入错误.最简单的解决方法是改用整数:

It's a rounding error related to the float values. The easiest way to fix it would to use integers instead:

$( "#sliderModal" ).slider({
    value:r.min_price,
    min: r.min_price,
    max: r.max_price,
    step:1,
    slide: function( event, ui ) {
        $( "#amountModal" ).val(ui.value*100);
    }
});

请注意,我已经删除了所有/100操作,并将该值乘以100.

Notice that I've removed all the /100 operations and multiplied the value by 100.

这假定该值未显示为数字-如果是,则将得到的数字乘以100,而不是要查找的美观,整洁的小数点后两位数字.

This assumes that the value is not being displayed as a number - if it is, you're going to get the number multiplied by 100 instead of the nice, neat two-decimal-place number you're looking for.

这篇关于jQuery UI滑块最大值不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆