Why does Firebug say toFixed() is not a function?

前端 未结 5 1670
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-23 19:57

I am using jQuery 1.7.2 and jQuery UI 1.9.1. I am using the code below within a slider. (http://jqueryui.com/slider/)

I have a function that should test two values a

相关标签:
5条回答
  • 2020-12-23 20:37

    toFixed isn't a method of non-numeric variable types. In other words, Low and High can't be fixed because when you get the value of something in Javascript, it automatically is set to a string type. Using parseFloat() (or parseInt() with a radix, if it's an integer) will allow you to convert different variable types to numbers which will enable the toFixed() function to work.

    var Low  = parseFloat($SliderValFrom.val()),
        High = parseFloat($SliderValTo.val());
    
    0 讨论(0)
  • 2020-12-23 20:43

    You need convert to number type:

    (+Low).toFixed(2)
    
    0 讨论(0)
  • 2020-12-23 20:50

    That is because Low is a string.

    .toFixed() only works with a number.


    Try doing:

    Low = parseFloat(Low).toFixed(..);
    
    0 讨论(0)
  • 2020-12-23 20:52

    Low is a string.

    .toFixed() only works with a number.

    A simple way to overcome such problem is to use type coercion:

    Low = (Low*1).toFixed(..);
    

    The multiplication by 1 forces to code to convert the string to number and doesn't change the value. JSFiddle code here.

    0 讨论(0)
  • 2020-12-23 20:59

    In a function, use as

    render: function (args) {
        if (args.value != 0)
            return (parseFloat(args.value).toFixed(2));
    
    
    },
    
    0 讨论(0)
提交回复
热议问题