2017-05-27 117 views
-1

我無法弄清楚是什麼導致10一直顯示爲紅色。其他數字的顯示方式應該如此,但只有在編號10時,如果語句未能比較正確的值。if語句(jQuery)

$(document).ready(function() { 
$("thead tr td:nth-child(3)").each(function(){ 

    var compare = $(this).text() 

     if(compare == 'null') { 
      $(this).text('No rating found').parent('tr').addClass("table-danger"); 
     } 
      else if (compare <= '5') { //Number 10 keeps comparing whit this statement  
         $(this).parent('tr').addClass("table-danger");  
      } 
      else if (compare > '5' && compare < '7') {  
         $(this).parent('tr').addClass("table-info");  
      } 
      else if (compare >= '7') {  
         $(this).parent('tr').addClass("table-success");  
      } 
    }); 
}); 

有什麼不對惠白第一else if聲明。和數量10應該是綠色的("table-success")

現場演示https://jsfiddle.net/gah1m33d/1/

回答

3

的問題是,你與5.嘗試的字符串值進行比較10多項]:

else if (parseInt(compare)<= 5) 
+1

問題已解決,但其他號碼如何顯示正常? – Slasher

+2

'10'以'1'開始,因此<'5'開始。 – inarilo