Javascript逻辑运算符未验证

Javascript逻辑运算符未验证,javascript,Javascript,简单的javascript逻辑运算符验证对我不起作用 目标价格不应大于原始价格 x = input from user; y = 1000; // fixed if(x > 1000 ) { alert ( x+'should not be greater than'+y); } else { alert ( ' proceed' ); } 这是我的这些值属于字符串类型,您需要将它们转换为数字。您可以使用Number() targetPrice = Number($('

简单的javascript逻辑运算符验证对我不起作用

目标价格不应大于原始价格

x = input from user;
y = 1000; // fixed

if(x > 1000 )
{
  alert ( x+'should not be greater than'+y);
}
else
{
  alert ( ' proceed' );
}

这是我的

这些值属于
字符串类型
,您需要将它们转换为
数字
。您可以使用
Number()

    targetPrice = Number($('#pdiwap_dropamt').val());
    orginalPrice = Number($('#orgprice_wap').val());

因为两个操作数都是字符串,所以它进行字符串比较

您可以将这些值转换为一个数字,然后进行类似的比较

    targetPrice = +$('#pdiwap_dropamt').val();
    orginalPrice = +$('#orgprice_wap').val();

你在争夺两条弦。用这样的东西

 if (parseInt(targetPrice) > parseInt(orginalPrice))

只需使用
parseInt
函数即可正确验证。Bucause html输入以字符串格式提供值

if (parseInt(targetPrice,10) > parseInt(orginalPrice,10)) 

我认为问题在于您得到的输入,它被视为一个字符串。在比较之前,请先尝试转换为整数


e、 g x=parseInt(用户输入)

是的!它的输入值是字符串,因此无法正确验证

试试这个

var x  = document.getElementById("input").value;
    var y  = 1000;
    if(!isNaN(x) && x < 1000) {
      alert ('proceed' );
    } else {
      alert(x+'should not be greater than'+y);
    }
var x=document.getElementById(“输入”).value;
变量y=1000;
如果(!isNaN(x)&&x<1000){
警报(“继续”);
}否则{
警报(x+'不应大于'+y');
}

如果(parseInt(targetPrice)>parseInt(originalprice))的话,您的示例在Chromeuse中运行良好