2017-05-27 118 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