我正在尝试写一个简单的测验。
我想创建一个问题数组,将其传递给循环并通过变量打印最终答案,该变量将为每个正确的答案计算20个分数。
function Myfunction() {
var y = ["q0",
"q1",
"q2",
"q3",
"q4"];
for (i = 0; i <= y.length; i++) {
var x = document.getElementById(y[i]).value("correct");
var score = x * 20;
alert("Your score is" & "score");
break;
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Welcome to your Web Quiz</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="robots" content="index,follow" />
<link rel="stylesheet" type="text/css" href="quiz.css" />
<form>
<h1>
Quiz about corporate crap
</h1>
<div class="questions">
What's the difference between a boss and a manager?
<ul><input type="radio" id="q0" value="correct">Boss is a boss</ul>
<ul><input type="radio" id="q0" value="False">Manager is a manager</ul>
<ul><input type="radio" id="q0" value="False">Both are blah </ul>
</div>
<br>
<div class="questions">
What's the right thing to do?
<br>
<ul><input type="radio" id="q1" value="correct">Do what the boss says </ul>
<ul><input type="radio" id="q1" value="False">Do what the bosses boss says</ul>
<ul><input type="radio" id="q1" value="False">Dont give a fuck do what you want to do</ul>
</div>
<br>
<div class="questions">
Listen to your heart is that good advice?
<ul><input type="radio" id="q2" value="correct">Listen to it, do not execute</ul>
<ul><input type="radio" id="q2" value="False">Shove it up your a#$$</ul>
<ul><input type="radio" id="q2" value="False"> Shove it up some one elses a$#$</ul>
<br>
</div>
<div class="questions">
<br>
Who is a client?
<ul><input type="radio" id="q3" value="correct">Someone whose astrological charts are right</ul>
<ul><input type="radio" id="q3" value="False">Who gets payed to screw around</ul>
<ul><input type="radio" id="q3" value="False">Someone who is arrogant about being stupid and proud about it </ul>
</div>
<br>
<br>
<br>
<input type="button" value="I am done" align="center" onsubmit= "Myfunction()">
</form>
</head>
<body>
</body>
</html>
答案 0 :(得分:2)
按钮没有onsubmit
事件,但表单确实如此。
尝试更改
<input type="button" value="I am done" align="center" onsubmit= "Myfunction()">
到
<input type="button" value="I am done" align="center" onclick="Myfunction()"/>
或者你可以改变
<form>
到
<form onsubmit="Myfunction()">
哪个更好,因为如果按下回车键,它会捕获提交事件