开发者

Strange behavior on comparing two select boxes values in jQuery

开发者 https://www.devze.com 2023-04-02 05:12 出处:网络
The HTML: <html> <body> <select class=\"FormInput\" id=\"minheight\" style=\"width: 120px;\" name=\"minheight\">

The HTML:

<html>
    <body>
        <select class="FormInput" id="minheight" style="width: 120px;" name="minheight">
    <option selected="selected" value="">- Select -</option>
    <option value=1>4' 0" (1.22 mts)</option>
    <option value=2>4' 1" (1.24 mts)</option>
    <option value=3>4' 2" (1.28 mts)</option>
    <option value=4>4' 3" (1.31 mts)</option>
    <option value=5>4' 4" (1.34 mts)</option>
    <option value=6>4' 5" (1.35 mts)</option>
    <option value=7>4' 6" (1.37 mts)</option>
    <option value=8>4' 7" (1.40 mts)</option>
    <option value=9>4' 8" (1.42 mts)</option>
    <option value=10>4' 9" (1.45 mts)</option>
    <option value=11>4' 10" (1.47 mts)</option>
    <option value=12>4' 11" (1.50 mts)</option>
    <option value=13>5' 0" (1.52 mts)</option>
    <option value=14>5' 1" (1.55 mts)</option>
    <option value=15>5' 2" (1.58 mts)</option>
    <option value=16>5' 3" (1.60 mts)</option>
    <option value=17>5' 4" (1.63 mts)</option>
    <option value=18>5' 5" (1.65 mts)</option>
    <option value=19>5' 6" (1.68 mts)</option>
    <option value=20>5' 7" (1.70 mts)</option>
    <option value=21>5' 8" (1.73 mts)</option>
    <option value=22>5' 9" (1.75 mts)</option>
    <option value=23>5' 10" (1.78 mts)</option>
    <option value=24>5' 11" (1.80 mts)</option>
    <option value=25>6' 0" (1.83 mts)</option>
    <option value=26>6' 1" (1.85 mts)</option>
    <option value=27>6' 2" (1.88 mts)</option>
    <option value=28>6' 3" (1.91 mts)</option>
    <option value=29>6' 4" (1.93 mts)</option>
    <option value=30>6' 5" (1.96 mts)</option>
    <option value=31>6' 6" (1.98 mts)</option>
    <option value=32>6' 7" (2.01 mts)</option>
    <option value=33>6' 8" (2.03 mts)</option>
    <option value=34>6' 9" (2.06 mts)</option>
    <option value=35>6' 10" (2.08 mts)</option>
    <option value=36>6' 11" (2.11 mts)</option>
    <option value=37>7' (2.13 mts) plus</option>
    </select> &nbsp; to &nbsp;
    <select class="FormInput" id="maxheight" style="width: 120px;" name="maxheight">
    <option selected="selected" value="">- Select -</option>
    <option value=1>4' 0" (1.22 mts)</option>
    <option value=2>4' 1" (1.24 mts)</option>
    <option value=3>4' 2" (1.28 mts)</option>
    <option value=4>4' 3" (1.31 mts)</option>
    <option value=5>4' 4" (1.34 mts)</option>
    <option value=6>4' 5" (1.35 mts)</option>
    <option value=7>4' 6" (1.37 mts)</option>
    <option value=8>4' 7" (1.40 mts)</option>
    <option value=9>4' 8" (1.42 mts)</option>
    <option value=10>4' 9" (1.45 mts)</option>
    <option value=11>4'开发者_如何学JAVA 10" (1.47 mts)</option>
    <option value=12>4' 11" (1.50 mts)</option>
    <option value=13>5' 0" (1.52 mts)</option>
    <option value=14>5' 1" (1.55 mts)</option>
    <option value=15>5' 2" (1.58 mts)</option>
    <option value=16>5' 3" (1.60 mts)</option>
    <option value=17>5' 4" (1.63 mts)</option>
    <option value=18>5' 5" (1.65 mts)</option>
    <option value=19>5' 6" (1.68 mts)</option>
    <option value=20>5' 7" (1.70 mts)</option>
    <option value=21>5' 8" (1.73 mts)</option>
    <option value=22>5' 9" (1.75 mts)</option>
    <option value=23>5' 10" (1.78 mts)</option>
    <option value=24>5' 11" (1.80 mts)</option>
    <option value=25>6' 0" (1.83 mts)</option>
    <option value=26>6' 1" (1.85 mts)</option>
    <option value=27>6' 2" (1.88 mts)</option>
    <option value=28>6' 3" (1.91 mts)</option>
    <option value=29>6' 4" (1.93 mts)</option>
    <option value=30>6' 5" (1.96 mts)</option>
    <option value=31>6' 6" (1.98 mts)</option>
    <option value=32>6' 7" (2.01 mts)</option>
    <option value=33>6' 8" (2.03 mts)</option>
    <option value=34>6' 9" (2.06 mts)</option>
    <option value=35>6' 10" (2.08 mts)</option>
    <option value=36>6' 11" (2.11 mts)</option>
    <option value=37>7' (2.13 mts) plus</option>
    </select>
    <span></span>
    </body>
</html>

The CSS:

.form-error { color:red; font-size:12px;}
.form-info { color:#B1B1B1; font-size:12px;}
.form-ok { color:green; font-size:12px; }

and the Script:

var minheight = $("#minheight");
var maxheight = $("#maxheight");
maxheight.blur(validatemaxheight);

$('#minheight,#maxheight').change(function(){  
        heightvalidate();
    });

function validatemaxheight()
    {
        heightvalidate();
    }

function heightvalidate()
    {
        console.log('minheight:: '+minheight.val()+'    maxheight:: '+maxheight.val());
        if(minheight.val() > maxheight.val())
        {
            minheight.parent().find('span').attr("class","form-error").html("Minimum height should not be greater than maximum height.");    
            minheight.focus();
            return false;
        }
        else if(minheight.val() == maxheight.val())
        {
            minheight.parent().find('span').attr("class","form-error").html("Minimum height should be less than maximum height.");    
            minheight.focus();
            return false;
        }
        else
        {
            minheight.parent().find('span').attr("class","form-ok").html('OK');
            return true;
        }    
    }

I am actually comparing the minimum height and maximum height. If the minheight > max height then, an error message will be shown.

The above condition works fine until if choose the minheight value above 9 and maxheight value greater than 9, which is greater than min height. Even though the minheight < maxheight, still the error message shows that the minheight is greater than maxheight.

The live version here: http://jsfiddle.net/prajan55/u9NJD/

Any help appreciated..


Use parseInt() on those values before comparing them to actually compare them as numbers not as strings.


I think this is because the check is doing this on strings and not integers, you have to convert them to integer first using parseInt(minheight.val()) > parseInt(maxheight.val())

0

精彩评论

暂无评论...
验证码 换一张
取 消