2012-08-08 85 views
1

我有一个jsFiddle来演示我的问题(并允许你们理清我)。JavaScript if()语句没有按预期进行评估

我只是检查两个输入文本框的值,并提醒用户,如果最大价格低于最低价格,但他们正在向后评估!我有,如果(maxValue < minValue)...,但它评估它,如果运营商是“是大于”。

我错过了什么?!?

<form id="search" action="search.php" method="post"> 

    <label id="lblPriceMin" for="txtPriceMin">Minimum Price</label> 
    <input type="text" id="txtPriceMin" name="priceMin"></input> 
    <br /> 
    <br /> 

    <label id="lblPriceMax" for="txtPriceMax">Maximum Price</label> 
    <input type="text" id="txtPriceMax" name="priceMax"></input> 
    <br /> 
    <br /> 

    <input type="reset" id="reset" name="reset" value="Clear Form" /> 
</form> 

这里的JS,

$('#txtPriceMax').focusout(function() { 

    var minValue = $('input#txtPriceMin').val(); 
    var maxValue = $('input#txtPriceMax').val(); 

    //alert ('minValue: ' + minValue + ', maxValue: ' + maxValue); 

    if (maxValue < minValue) { 
     alert ('The maximum value (' + maxValue + ') must be greater than the minimum value (' + minValue + ')!'); 
    } 
}); 
+0

可能重复[从意外的结果,从文本输入值的比较 “大于”] (http://stackoverflow.com/questions/8475846/unexpected-result-from-greater-than-compari儿子的价值从文本输入) – 2012-08-08 18:43:38

回答

8

使用parseFloat()

jsFiddle Example

if (parseFloat(maxValue) < parseFloat(minValue)) 
+1

啊!所以我比较了字符串!谢谢,Gabe! – marky 2012-08-08 18:45:19

+0

@eventide你打赌,很高兴我能帮上忙。 – Gabe 2012-08-08 18:45:52