Javascript 如果语句未触发警报-表单/测验
脚本应该会带来一个Javascript警报,提示正确或不正确的答案,但警报不会弹出。有什么问题吗?我有一种感觉,这是一个简单的解决办法,我只是似乎找不到它。我已经验证了整个页面(这只是一个片段),它通过了验证Javascript 如果语句未触发警报-表单/测验,javascript,forms,if-statement,alert,Javascript,Forms,If Statement,Alert,脚本应该会带来一个Javascript警报,提示正确或不正确的答案,但警报不会弹出。有什么问题吗?我有一种感觉,这是一个简单的解决办法,我只是似乎找不到它。我已经验证了整个页面(这只是一个片段),它通过了验证 <script type="text/javascript"> /* <![CDATA[ */ /* ]]> */ function scoreQuestion1(answer){ if (answer == "a")
<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>
/* */
功能评分问题1(答案){
如果(答案=“a”)
窗口提示(“正确答案”);
如果(答案=“b”)
窗口提示(“回答不正确”);
如果(答案=“c”)
窗口提示(“回答不正确”);
如果(答案=“d”)
窗口提示(“回答不正确”);
}
功能评分问题2(答案){
如果(答案=“a”)
窗口提示(“回答不正确”);
如果(答案=“b”)
窗口提示(“回答不正确”);
如果(答案=“c”)
窗口提示(“正确答案”);
如果(答案=“d”)
窗口提示(“回答不正确”);
}
功能评分问题3(答案){
如果(答案=“a”)
窗口提示(“回答不正确”);
如果(答案=“b”)
窗口提示(“正确答案”);
如果(答案=“c”)
窗口提示(“回答不正确”);
如果(答案=“d”)
窗口提示(“回答不正确”);
功能评分问题4(答案){
如果(答案=“a”)
窗口提示(“回答不正确”);
如果(答案=“b”)
窗口提示(“回答不正确”);
如果(答案=“c”)
窗口提示(“正确答案”);
如果(答案=“d”)
窗口提示(“回答不正确”);
}
功能评分问题5(答案){
如果(答案=“a”)
窗口提示(“回答不正确”);
如果(答案=“b”)
窗口提示(“回答不正确”);
如果(答案=“c”)
窗口提示(“回答不正确”);
如果(答案=“d”)
窗口提示(“正确答案”);
}
1.有多少种自然元素?
92
113
103
88
2.如果一千克空气从1m3压缩到0.5m3,以下哪项陈述是正确的?
密度减半。
质量减半。
密度增加了一倍。
质量增加了一倍。
3.重力引起的加速度是多少?
980 m/s2
9.8米/平方米
98米/秒2
0.98 m/s2
4.什么是国际单位制密度单位?
cm3/g
m3/kg
千克/立方米
g/cm3
5.哪一种密度最高?
铅
水
水银
钨
您从未关闭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");
}
你错过了分数问题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) 使用{括号
if(answer=="a"){
alert("Incorrect Answer");
}
虽然您可以省略它们,并且if语句将只使用下一行,但它变得不那么清晰
3) 否则
我想这将出现在下一课中,但代码可以大大简化为:
function scoreQuestion3(answer){
if(answer=="a"){
alert("Correct Answer!");
}else{
alert("Incorrect answer I'm afraid!");
}
}
这是大量的代码。你最好了解数组,你的代码会大大减少!当然,我正在努力学习javascript类,我正在努力确保我理解如果出现问题,为什么事情不起作用。叮叮叮叮。非常感谢。我疲惫的眼睛没有注意到。非常感谢,新鲜的眼睛是最好的选择最好的。