我不知道这是什么意思SyntaxError:预期的表达式,得到了关键字“ else”

时间:2018-10-30 15:40:30

标签: jquery

您可以检查我的代码吗?我的if语句中有错误。 这是什么意思 SyntaxError:预期的表达式,关键字为“ else”

$ (document).ready(function($)
{
    var sum = 0;
    var numberQA = 4

    $('.skillA:checkbox').click(function()
    {
        sum = 0;

        $('.skillA:checkbox:checked').each(function(idx,elm)
        {
            // idx: index position of selector, elm = this selector,
            sum += parseInt(elm.value, 10);
        });

        // average = sum / numberQA;
        $('#totalSkillA').html(sum);

        if (4 <= sum <=6)
        {        
            $('#communicationSkill1').css({
                "backgroundColor":"#087cb7",
                "color":"white"
            });

        else if (7 <= sum <=10)
        {        
            $('#communicationSkill2').css({
                "backgroundColor":"#087cb7",
                "color":"white"
            });

        else if (11 <= sum <=13)
        {
            $('#communicationSkill3').css({
                "backgroundColor":"#087cb7",
                "color":"white"
            });    

        else if (14 <= sum <= 16)
        {        
            $('#communicationSkill4').css({
                "backgroundColor":"#087cb7",
                "color":"white"
            });
    }
});

2 个答案:

答案 0 :(得分:2)

您应该对编程进行更多的训练。

  1. 4<=sum<=6是错误的。将其更改为4 <= sum && sum <= 6(读JavaScript Operators
  2. 您必须在if和else if块关闭:(读JavaScript if else and else if

    if ( /* condition*/ )
    {
        // statements
    }
    else if ( /* condition*/ )
    {
        // statements
    }
    else
    {
        // statements
    }
    

答案 1 :(得分:0)

认为您需要像这样的东西

if (4 <= sum <= 6) {
    $('#communicationSkill1').css({"backgroundColor": "#087cb7", "color": "white"})
}
else if (7 <= sum <= 10) {
    $('#communicationSkill2').css({"backgroundColor": "#087cb7", "color": "white"})
}
else if (11 <= sum <= 13) {
    $('#communicationSkill3').css({"backgroundColor": "#087cb7", "color": "white"})
}
else if (14 <= sum <= 16) {
    $('#communicationSkill4').css({"backgroundColor": "#087cb7", "color": "white"})
} else 
{
    ....
}