2015-06-01 112 views
0

一个简单的javascript逻辑运算符验证不适用于我。Javascript逻辑运算符不验证

例如。目标价应不低于原单价格

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

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

这里是我的EXAMPLE

+0

您的示例工作正常,在Chrome – Turnip

+0

使用,如果(parseInt函数(targetPrice)> parseInt函数(orginalPrice)) –

回答

0

这些价值string型的大,你需要将它们转换为number。你可以使用​​。

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

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

您可以将值转换为一个数字,然后不喜欢

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

您compering两个字符串进行比较。使用像这样的东西

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

只需使用parseInt函数来正确验证。 Bucause html输入以字符串格式给出值。

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

Updated Link

0

我认为这个问题是这被看作是一个字符串,你得到的输入。比较前先尝试转换为整数。

例如x = parseInt(来自用户的输入);

+0

退房此更新https://jsfiddle.net/5t422enw/3/ – neddinn

0

是的!它的输入值是一个字符串,所以它没有正确验证。

试试这个。

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