gpt4 book ai didi

javascript 无法识别 1 var >= another

转载 作者:行者123 更新时间:2023-11-28 12:06:46 25 4
gpt4 key购买 nike

所以,我的问题是我的代码似乎识别出 100 是 < 2000,但它无法识别 200 < 1000

这是我的代码(我也使用jquery作为框架仅供引用)

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

if (!isNaN(price_from) && !isNaN(price_to) && (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);
}

});

我尝试在变量上使用 parseInt() 但没有解决任何问题。

最佳答案

抱歉,但您确实需要这样做:

var price_from = parseInt($('.filter-price #price_from').val(), 10);
var price_to = parseInt($('.filter-price #price_to').val(), 10);

在chrome控制台上查看结果:

'200' >= '1000'
true

200 >= 1000
false

如果您不想将数字限制为 int,请将 parseInt(val, 10) 替换为 parseFloat(val)

关于javascript 无法识别 1 var >= another,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8290010/

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