为什么Firebug说toFixed()不是函数? [英] Why does Firebug say toFixed() is not a function?

查看:82
本文介绍了为什么Firebug说toFixed()不是函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery 1.7.2和jQuery UI 1.9.1。我在滑块内使用下面的代码。 (http://jqueryui.com/slider/)

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/)

我有一个应该测试两个值的函数,并根据两个值之间的差异重新格式化它们(对于适当的小数位)。如果差异大于10,我将解析整数。如果差值大于5,则应保留一个小数。其他一切,我会保留两位小数。

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. If the difference is greater than 5, it should keep one decimal. Everything else, I will keep two decimals.

当我输入两个差值为10或更小的值时,我使用toFixed()函数。并且,在Firebug中,我看到一个错误:

When I enter two values that have a difference that is ten or less, I use the toFixed() function. And, in Firebug, I see an error:

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

有什么简单的我做错了吗?

Is there something simple that I am doing wrong?

这是我的代码:

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 不是非数字变量类型的方法。换句话说,无法修复,因为当你在Javascript中获得某些东西的价值时,它自动设置为字符串类型。使用 parseFloat()(或带基数的 parseInt(),如果它是一个整数)将允许你转换不同的变量类型到数字将使 toFixed()函数起作用。

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());

这篇关于为什么Firebug说toFixed()不是函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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