简体   繁体   English

为什么 Firebug 说 toFixed() 不是函数?

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

I am using jQuery 1.7.2 and jQuery UI 1.9.1.我正在使用 jQuery 1.7.2 和 jQuery UI 1.9.1。 I am using the code below within a slider.我在滑块中使用下面的代码。 (http://jqueryui.com/slider/) (http://jqueryui.com/slider/)

I have a function that should test two values and depending on the difference between the two values reformat them (to the appropriate decimal place).我有一个函数应该测试两个值,并根据两个值之间的差异重新格式化它们(到适当的小数位)。 If the difference is greater than 10, I will parse out the integer.如果差值大于 10,我将解析出整数。 If the difference is greater than 5, it should keep one decimal.如果差值大于 5,则应保留一位小数。 Everything else, I will keep two decimals.其他一切,我将保留两位小数。

When I enter two values that have a difference that is ten or less, I use the toFixed() function.当我输入两个相差小于等于 10 的值时,我使用 toFixed() 函数。 And, in Firebug, I see an error:而且,在 Firebug 中,我看到一个错误:

TypeError: Low.toFixed is not a function
Low = Low.toFixed(2);

Is there something simple that I am doing wrong?有什么简单的我做错了吗?

Here is my code:这是我的代码:

var Low = $SliderValFrom.val(),
High = $SliderValTo.val();

// THE NUMBER IS VALID
if (isNaN(Low) == false && isNaN(High) == false) {
    Diff = High - Low;
if (Diff > 10) {
      Low = parseInt(Low);  
  High = parseInt(High);    
} else if (Diff > 5) {
       Low = Low.toFixed(1);
       High = High.toFixed(1);
} else {
       Low = Low.toFixed(2);
   High = High.toFixed(2);
}
}

toFixed isn't a method of non-numeric variable types. toFixed不是非数字变量类型的方法。 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.换句话说, LowHigh无法固定,因为当您在 Javascript 中获取某物的值时,它会自动设置为字符串类型。 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.使用parseFloat() (或带有基数的parseInt() ,如果它是一个整数)将允许您将不同的变量类型转换为数字,这将使toFixed()函数能够工作。

var Low  = parseFloat($SliderValFrom.val()),
    High = parseFloat($SliderValTo.val());

That is because Low is a string.那是因为Low是一个字符串。

.toFixed() only works with a number. .toFixed()仅适用于数字。


Try doing:尝试做:

Low = parseFloat(Low).toFixed(..);

Low is a string. Low是一个字符串。

.toFixed() only works with a number. .toFixed()仅适用于数字。

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.乘以 1 会强制代码将字符串转换为数字并且不会更改值。

您需要转换为数字类型:

(+Low).toFixed(2)

parseFloat() will return NaN for empty string, why not using Number() function instead? parseFloat()将为空字符串返回NaN ,为什么不使用Number()函数呢?

Values of other types can be converted to numbers using the Number() function.其他类型的值可以使用Number()函数转换为数字。

parseFloat('').toFixed(2) // "NaN"
Number('').toFixed(2) // "0.00"

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number

In a function, use as在函数中,用作

render: function (args) {
    if (args.value != 0)
        return (parseFloat(args.value).toFixed(2));


},
Low = Number(Low).toFixed(1);

添加Number函数以将Low转换为数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM