if终止的未终止字符串常量

时间:2013-08-09 16:37:41

标签: javascript jquery

我正在编写一个测验,需要根据分数显示一定的答案。

当我刚刚显示得分时,我有这个工作,但现在我已经添加了if else,它已停止工作,我得到一个未终止的字符串常量

$total_score = $first_question + $second_question + $third_question + $fourth_question + $fifth_question + $sixth_question + $seventh_question + $eighth_question + $ninth_question + $tenth_question + $eleventh_question + $twelfth_question + $thirteenth_question + $fourteenth_question + $fifthteenth_question + $sixteenth_question ;
});

$("#btn").click(function() {
   $('#reveal').html("<h3><strong>Total score</strong> " + $total_score +"</h3>"); 

if ($total_score >= 13) {
$('#answer').html("<h3><strong>13-16: Answer</strong></h3>"); 

} else if ($total_score >=9 && $total_score <= 12) {
$('#answer').html("<h3><strong>9-12: answer</strong></h3>"); 

} else if ($total_score >=5 && $total_score <= 8) {
$('#answer').html("<h3><strong>5-8: answer</strong></h3>"); 


 } else {
 $('#answer').html("<h3><strong> everything else</strong></h3>"); 
 }

});

2 个答案:

答案 0 :(得分:0)

Javascript将行结尾粗略地视为分号(;),因此您不能像脚本中那样拥有多行字符串。删除它们,它会没事的。

答案 1 :(得分:0)

除了@Spork指出的换行符之外,你需要在$ total_score计算后删除无关的“})”:

    $total_score = $first_question + $second_question + $third_question + $fourth_question + $fifth_question + $sixth_question + $seventh_question + $eighth_question + $ninth_question + $tenth_question + $eleventh_question + $twelfth_question + $thirteenth_question + $fourteenth_question + $fifthteenth_question + $sixteenth_question ;
    /* must remove the following:
    });
    */
    $("#btn").click(function() {
        $('#reveal').html("<h3><strong>Total score</strong> " + $total_score +"</h3>"); 

        if ($total_score >= 13) {
            $('#answer').html("<h3><strong>13-16: Answer </strong></h3>"); 

        } else if ($total_score >=9 && $total_score <= 12) {
            $('#answer').html("<h3><strong>9-12: answer </strong></h3>"); 

        } else if ($total_score >=5 && $total_score <= 8) {
            $('#answer').html("<h3><strong>5-8: answer </strong></h3>"); 
        } else {
            $('#answer').html("<h3><strong> everything else</strong></h3>"); 
        }

    });