该脚本应该带来正确或错误答案的Javascript警报,警报不会弹出。可能是什么问题呢?我有一种感觉,这是一个简单的解决方案,我似乎无法找到它。我已经验证了整个页面(这只是一个片段),它通过了验证。
<script type="text/javascript">
/* <![CDATA[ */
/* ]]> */
function scoreQuestion1(answer){
if (answer == "a")
window.alert("Correct Answer");
if (answer == "b")
window.alert("Incorrect Answer");
if (answer == "c")
window.alert("Incorrect Answer");
if (answer == "d")
window.alert("Incorrect Answer");
}
function scoreQuestion2(answer){
if (answer == "a")
window.alert("InCorrect Answer");
if (answer == "b")
window.alert("Incorrect Answer");
if (answer == "c")
window.alert("Correct Answer");
if (answer == "d")
window.alert("Incorrect Answer");
}
function scoreQuestion3(answer){
if (answer == "a")
window.alert("Incorrect Answer");
if (answer == "b")
window.alert("Correct Answer");
if (answer == "c")
window.alert("Incorrect Answer");
if (answer == "d")
window.alert("Incorrect Answer");
function scoreQuestion4(answer){
if (answer == "a")
window.alert("Incorrect Answer");
if (answer == "b")
window.alert("Incorrect Answer");
if (answer == "c")
window.alert("Correct Answer");
if (answer == "d")
window.alert("Incorrect Answer");
}
function scoreQuestion5(answer){
if (answer == "a")
window.alert("Incorrect Answer");
if (answer == "b")
window.alert("Incorrect Answer");
if (answer == "c")
window.alert("Incorrect Answer");
if (answer == "d")
window.alert("Correct Answer");
}
</script>
</head>
<body>
<form action="" name="quiz">
<p><strong>1. How many natural elements are there?</strong></p><p>
<input type="radio" name="question1" value="a" onclick="scoreQuestion1('a')" />92<br /> <!-- correct answer-->
<input type="radio" name="question1" value="b" onclick="scoreQuestion1('b')" />113<br />
<input type="radio" name="question1" value="c" onclick="scoreQuestion1('c')" />103<br />
<input type="radio" name="question1" value="d" onclick="scoreQuestion1('d')" />88<br /></p>
<p><strong>2. If one kg of air is compressed from 1m3 to 0.5 m3, which of the following statements is true?</strong></p><p>
<input type="radio" name="question2" value="a" onclick="scoreQuestion2('a')" />The density is halved.<br />
<input type="radio" name="question2" value="b" onclick="scoreQuestion2('b')" />The mass is halved.<br />
<input type="radio" name="question2" value="c" onclick="scoreQuestion2('c')" />The density is doubled.<br /> <!--correct answer-->
<input type="radio" name="question2" value="d" onclick="scoreQuestion2('d')" />The mass is doubled.<br /></p>
<p><strong>3. What is the acceleration due to gravity?</strong></p><p>
<input type="radio" name="question3" value="a" onclick="scoreQuestion3('a')" />980 m/s2<br />
<input type="radio" name="question3" value="b" onclick="scoreQuestion3('b')" />9.8 m/s2<br /> <!--correct answer-->
<input type="radio" name="question3" value="c" onclick="scoreQuestion3('c')" />98 m/s2<br />
<input type="radio" name="question3" value="d" onclick="scoreQuestion3('d')" />0.98 m/s2<br /></p>
<p><strong>4. What is the SI unit of density?</strong></p><p>
<input type="radio" name="question4" value="a" onclick="scoreQuestion4('a')" />cm3/g<br />
<input type="radio" name="question4" value="b" onclick="scoreQuestion4('b')" />m3/kg<br />
<input type="radio" name="question4" value="c" onclick="scoreQuestion4('c')" />kg/m3<br /> <!--correct answer-->
<input type="radio" name="question4" value="d" onclick="scoreQuestion4('d')" />g/cm3<br /></p>
<p><strong>5. Which of these has the highest density?</strong></p><p>
<input type="radio" name="question5" value="a" onclick="scoreQuestion5('a')" />Lead<br />
<input type="radio" name="question5" value="b" onclick="scoreQuestion5('b')" />Water<br />
<input type="radio" name="question5" value="c" onclick="scoreQuestion5('c')" />Mercury<br />
<input type="radio" name="question5" value="d" onclick="scoreQuestion5('d')" />Tungsten<br /></p> <!--correct answer-->
</form>
答案 0 :(得分:1)
您永远不会关闭scoreQuestion3的功能,这会导致解析错误。如果你解决了这个问题,它应该可行。
function scoreQuestion3(answer){
if (answer == "a")
window.alert("Incorrect Answer");
if (answer == "b")
window.alert("Correct Answer");
if (answer == "c")
window.alert("Incorrect Answer");
if (answer == "d")
window.alert("Incorrect Answer");
}
答案 1 :(得分:1)
你错过了一个关闭你的得分问题3。
你应该研究一些事情:
1)正确的缩进
如果你在下一层(就像这样)时缩进,那么当发生这类错误时就会变得更加明显。
function scoreQuestion3(answer){
if (answer == "a")
window.alert("Incorrect Answer");
if (answer == "b")
window.alert("Correct Answer");
if (answer == "c")
window.alert("Incorrect Answer");
if (answer == "d")
window.alert("Incorrect Answer");
function scoreQuestion4()...
您可以立即看到它已经失控并相应地进行调整。
直接从那里得到的东西:
2)使用{bracket
if(answer=="a"){
alert("Incorrect Answer");
}
虽然你可以省略它们,if语句只会使用下一行,但它变得不那么清楚了。
3)其他
我想这是在你的下一课,但代码可以大大简化为:
function scoreQuestion3(answer){
if(answer=="a"){
alert("Correct Answer!");
}else{
alert("Incorrect answer I'm afraid!");
}
}