我正在使用网站的html和javascript编写测验。
我的问题是,当我运行测验以查看结果时,仅显示默认值。无论我如何回答测验问题,都会发生这种情况。有人看到我可以做些什么来解决它吗?每次回答问题时,应该更新不同结果的总数(如果问题答案与结果相对应,则将结果加1;否则将结果不加1)。我认为我的if语句有一些需要更改的地方。我玩过它,但似乎无法弄清楚。
<!DOCTYPE html>
<html>
<head>
<title>Quiz</title>
<style type = "text/css">
em { font-weight: bold;
color: black; }
p { font-size: 12pt;
font-family: times new roman, sans-serif; color: black; }
b { font-size: 13pt;
font-family: times new roman, sans-serif; color: black; }
</style>
<script language="Javascript">
function process()
{
var A = 0;
var B = 0;
var C = 0;
var D = 0;
var f = document.f;
var i = 0;
for (i = 0; i < f.q1.length; i++) if (f.q1[i].checked) value = f.q1[i].value;
if (value == "1") { } /*corresponds to the answer being chosen, i.e. first answer*/
if (value == "2") { C++; A++; B++; }
if (value == "3") { C++; D++; A++; B++; }
for (i = 0; i < f.q2.length; i++) if (f.q2[i].checked) value = f.q2[i].value;
if (value == "1") { C++; D++; A++; }
if (value == "2") { D++; A++; B++; }
if (value == "3") { C++; A++; B++; }
if (value == "4") { C++; D++; A++; B++; }
for (i = 0; i < f.q3.length; i++) if (f.q3[i].checked) value = f.q3[i].value;
if (value == "1") { D++; }
if (value == "2") { }
for (i = 0; i < f.q4.length; i++) if (f.q4[i].checked) value = f.q4[i].value;
if (value == "1") { A++; }
if (value == "2") { A++; B++; }
if (value == "3") { C++; D++; A++; B++; }
/*same format for all other questions*/
var out = "A"; /*default value*/
i = "A"; /*default value*/
if (C > i) { out ="C"; i = "C"; }
if (B > i) { out ="B"; i = "B"; }
if (D > i) { out ="D"; i = "D"; }
window.alert ("Based on your answers, your result is " + i + " !!"); } /*right now, i always outputs as A*/
</script>
</head>
<body>
<?php include ('menu.html'); ?>
<? include ("file.php"); ?>
<br><p>Answer the questions below</p> <br>
<form name="f" method= "post" action= "file.php">
<p><b>1. Question1 Text?<br></b>
<input type="radio" name="q1" id="ans" value="1">Q1 Ans1<br>
<input type="radio" name="q1" id="ans" value="2">Q1 Ans2<br>
<input type="radio" name="q1" id="ans" value="3">Q1 Ans3<br><br>
<b>2. Question2 Text?<br></b>
<input type="radio" name="q2" id="ans" value="1">Q2 Ans1<br>
<input type="radio" name="q2" id="ans" value="2">Q2 Ans2<br>
<input type="radio" name="q2" id="ans" value="3">Q2 Ans3<br>
<input type="radio" name="q2" id="ans" value="4">Q2 Ans4<br><br>
<b>3. Question3 Text?<br></b>
<input type="radio" name="q3" id="ans" value="1">Q3 Ans1<br>
<input type="radio" name="q3" id="ans" value="2">Q3 Ans2<br><br>
<b>4. Question4 Text?<br></b>
<input type="radio" name="q4" id="ans" value="1">Q4 Ans1<br>
<input type="radio" name="q4" id="ans" value="2">Q4 Ans2<br>
<input type="radio" name="q4" id="ans" value="3">Q4 Ans3<br><br>
/*more questions with same format*/
<p>Thanks for taking the quiz.<p><br>
<input type="button" value="Result" onclick="process();"><br><br>
</form>
<div class="footer"><?php include('myfooter.html');?></div>
</body>
</html>
最佳答案
我看到的问题是,在下面的示例for循环中,错误是仅第一个if语句将包含在for循环中:
因此,下面的代码是:
for (i = 0; i < f.q1.length; i++) if (f.q1[i].checked) value = f.q1[i].value;
if (value == "1") { } /*corresponds to the answer being chosen, i.e. first answer*/
if (value == "2") { C++; A++; B++; }
if (value == "3") { C++; D++; F++; B++; }
实际上这样做:
for (i = 0; i < f.q1.length; i++) {
if (f.q1[i].checked) {
value = f.q1[i].value;
}
}
if (value == "1") { } /*corresponds to the answer being chosen, i.e. first answer*/
if (value == "2") {
C++; A++; B++;
}
if (value == "3") {
C++; D++; F++; B++;
}
请调整代码,使所有if语句都在for循环的范围内:
for (i = 0; i < f.q1.length; i++) {
if (f.q1[i].checked) {
value = f.q1[i].value;
}
if (value == "1") { } /*corresponds to the answer being chosen, i.e. first answer*/
if (value == "2") { C++; A++; B++; }
if (value == "3") { C++; D++; F++; B++; }
}
通常,由于上述原因,即使对于单个语句也忽略{},这是一个不好的做法。
另外,以下代码:
var out = "A"; /*default value*/
i = "A"; /*default value*/
会以“ out”和“ i”变量结尾并接收字符串“ A”。您希望他们成为:
var out = "A"; /*default value*/
i = A; /*default value*/
并且还要更改下面的其余赋值语句以除去“”,以使它们接收值而不是字符串:
var out = "A";
i = A;
if (C > i) {
out = "C"; i = C;
}else if (B > i) {
out = "B"; i = B;
}else if (D > i) {
out = "D"; i = D;
}
alert ("Based on your answers, your result is " + out + " !!");
还有一件事,第一个for循环包含一个简短的语句“ F ++”,应改为“ A ++”:
if (value == "3") { C++; D++; A++; B++; }
请参见下面的工作代码:
JSfiddle
关于javascript - JavaScript仅在if中输出默认值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23190843/