0

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

当我刚刚显示分数时,我有这个工作,但现在我已经添加了 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>"); 
 }

});
4

2 回答 2

0

Javascript 将行尾大致视为分号 (;),因此您不能像在脚本中那样使用多行字符串。删除它们,它会没事的。

于 2013-08-09T16:42:18.993 回答
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>"); 
        }

    });
于 2013-08-09T16:50:28.897 回答