2011-09-01 112 views
1

值HTML:奇怪的行爲,jQuery的

<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' 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> 

的CSS:

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

和腳本:

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

我其實比較最低高度和最大高度。如果minheight> max height,則會顯示錯誤消息。直到如果選擇上述9中的值了minHeight和maxheight值大於9,這是比最小高度大

上述條件工作正常。即使最大高度,仍然錯誤信息顯示minheight大於maxheight。

的真人版在這裏: http://jsfiddle.net/prajan55/u9NJD/

任何幫助表示讚賞..

回答

1

我想這是因爲檢查是這樣做的字符串,而不是整數,你必須將它們轉換爲整數首先使用parseInt函數(minheight.val())> parseInt函數(maxheight.val())

+0

現在工作得很好。謝謝薩米爾。 – prajan55

+0

太棒了!不要忘記投票;) –

3

使用parseInt函數()上的比較它們實際上他們比較的數字而不是字符串之前這些值。

+0

明白了。感謝WTK。 – prajan55

+0

很高興幫助:) – WTK