gpt4 book ai didi

javascript - price_from.toFixed() 不是函数

转载 作者:搜寻专家 更新时间:2023-11-01 04:44:08 25 4
gpt4 key购买 nike

这里的问题很简单,以前可以,但现在不行了。

这是我的代码:

$('.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);
}

});

现在 Web 开发人员给我错误“price_from.toFixed 不是一个函数”,我的 javascript 不工作。

最佳答案

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

Number(price_from).toFixed();

注意:您正在使用 isNaN 检查字符串是否包含数字。这是有效的,因为 isNaN 在测试之前进行了隐式数字转换。但是,要使用任何一种方法,您都需要明确地进行相同的转换,如上所示。

不要混淆对象的 JavaScript 类型 和它所代表的内容。字符串可以包含字符串形式的“数字”,但仍然只是一个字符串。

关于javascript - price_from.toFixed() 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8633129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com