if 语句 (jQuery)

杀戮者

我无法弄清楚,是什么导致10它一直显示为红色。其他数字按应有的方式显示,但10如果语句无法比较正确的值,则仅在数字上显示

$( 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/

赛跑者

问题是您将 10 [a number] 与字符串值 5 进行比较。尝试:

else if ( parseInt(compare) <= 5 )

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章