javascript price_from.toFixed() 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8633129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:03:38  来源:igfitidea点击:

price_from.toFixed() is not a function

javascriptjquery

提问by scarhand

pretty simple issue here, this was working before but not anymore.

这里很简单的问题,这以前有效但不再有效。

heres my code:

继承人我的代码:

$('.filter-price').submit(function(e) {

    var alert_message = '';
    var price_from = $('.filter-price #price_from').val();
    var price_to = $('.filter-price #price_to').val();

    if (isNaN(price_from) || isNaN(price_to))
    {
        if (isNaN(price_from))
        {
            alert_message += "Price from must be a number, i.e. 500\n";
            $('.filter-price #price_from').val('From');
        }

        if (isNaN(price_to))
        {
            alert_message += "Price to must be a number, i.e. 500\n";
            $('.filter-price #price_to').val('To');
        }
    }
    else
    {
        price_from = price_from.toFixed();
        price_to = price_to.toFixed();

        if (price_from >= price_to)
        {
            alert_message += "Price from must be less than price to\n";
            $('.filter-price #price_from').val('From');
            $('.filter-price #price_to').val('To');
        }
    }

    if (alert_message != '')
    {
        e.preventDefault();
        alert(alert_message);
    }

});

now web developer is giving me the error "price_from.toFixed is not a function" and my javascript is not working.

现在 Web 开发人员给了我错误“price_from.toFixed 不是函数”并且我的 javascript 无法正常工作。

回答by Wayne

valreturns a string; toFixedoperates on numbers. Convert your string to a number like this:

val返回一个字符串;toFixed对数字进行操作。将您的字符串转换为这样的数字:

Number(price_from).toFixed();

Note:You're checking whether the string contains a number using isNaN. This works because isNaNdoes an implicit number conversion before testing. To use any of the number methods, however, you'll need to do this same conversion explicitly, as shown above.

注意:您正在使用isNaN. 这是有效的,因为isNaN在测试之前进行了隐式数字转换。但是,要使用任何数字方法,您需要显式执行相同的转换,如上所示。

Don't confuse the JavaScript typeof the object with what it represents. A string can contain a "number" in string form and still be just a string.

不要将对象的 JavaScript类型与其代表的内容混淆。一个字符串可以包含一个字符串形式的“数字”,并且仍然只是一个字符串。

回答by Engineer

First of all 'isNaN' function does not REALLY check whether string represents number. For example isNaN('456a') returns true, but '456a' is not number at all. For this purpose you need other method of checking. I would suggest to use regular expressions.

首先,'isNaN' 函数并没有真正检查字符串是否代表数字。例如 isNaN('456a') 返回 true,但 '456a' 根本不是数字。为此,您需要其他检查方法。我建议使用正则表达式。

Then you need to parse string for compareing numbers ( i.e. price_from < price_to ).

然后你需要解析字符串来比较数字(即 price_from < price_to )。

Here is the modificated code you may assume:

这是您可以假设的修改后的代码:

$('.filter-price').submit(function(e) {    

    var alert_message = '';    
    var price_from = $('.filter-price #price_from').val();
    var price_to = $('.filter-price #price_to').val();

    var isNumberRegExp = new RegExp(/^[-+]?[0-9]+(\.[0-9]+)*$/);

    if (!isNumberRegExp.test(price_from) || !isNumberRegExp.test(price_to))
    {
        if (!isNumberRegExp.test(price_from))
        {
            alert_message += "Price from must be a number, i.e. 500\n";
            $('.filter-price #price_from').val('From');
        }

        if (!isNumberRegExp.test(price_to))
        {
            alert_message += "Price to must be a number, i.e. 500\n";
            $('.filter-price #price_to').val('To');
        }
    }
    else
    {
        price_from = parseFloat(price_from);
        price_to = parseFloat(price_to);

        if (price_from >= price_to)
        {
            alert_message += "Price from must be less than price to\n";
            $('.filter-price #price_from').val('From');
            $('.filter-price #price_to').val('To');
        }
    }

    if (alert_message != '')
    {
        e.preventDefault();
        alert(alert_message);
    }

});