对于学校项目我试图通过javascript创建一个测验。前提是“你应该投票”是我们问基本的公民问题,然后根据分数显示一条消息。例如,如果您有10个问题中的3个,则会显示一条消息,说“开国元勋不满意”,或者如果您得到7分中的7分,则会说“如果您愿意,请继续投票”。我相信我有径向按钮和得分计数器,但是我在最后阶段遇到了麻烦。即基于分数的不同消息。我相信我可能需要添加另一个功能,但其他人告诉我,我没有。无论如何,代码低于任何帮助将不胜感激。
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>test</title>
<style type="text/css">
.a1 {
}
</style>
<script type="text/javascript">
var score = 0;
function fsubmit()
{
var correct1 = document.getElementById("a1")
if (correct1.checked === true)
{
score ++;
}
var correct2 = document.getElementById("b4")
if (correct2.checked === true)
{
score ++;
}
}
// And here
</script>
</head>
<body>
Question 1: What is the Surpreme law of the land?
<table width="200" border="1">
<tbody>
<tr>
<td><input type="radio" name="input_1" id="a1" value="a1" tabindex="1"> U.S Constitution </td>
<td><input type="radio" name="input_2" id="a2" value="a2" tabindex="2"> Declaration of Independence</td>
</tr>
</tbody>
</table>
<table width="200" border="1">
<tbody>
<tr> </tr>
<tr>
<td><input type="radio" name="input_3" id="a3" value="a3" tabindex="3">Bill of Rights</td>
<td><input type="radio" name="input_4" id="a4" value="a4" tabindex="4">Mayflower Compact</td>
</tr>
</tbody>
</table>
Question 9: What are the first ten amendments to the U.S Constitution?
<table width="200" border="1">
<tbody>
<tr>
<td><input type="radio" name="input_5" id="b1" value="b1" tabindex="5"> Oprah's Favorite Things</td>
<td><input type="radio" name="input_6" id="b2" value="b2" tabindex="6"> The list of Ten</td>
</tr>
</tbody>
</table>
<table width="200" border="1">
<tbody>
<tr> </tr>
<tr>
<td><input type="radio" name="input_7" id="b3" value="b3" tabindex="7">The Ten Commondments</td>
<td><input type="radio" name="input_8" id="b4" value="b4" tabindex="8">The Bill of Rights</td>
</tr>
</tbody>
</table>
<input type="Submit" name="btnsubmit" id="btnsubmit" value="submit"
onclick="fsubmit()"/>
</body>
</html>
答案 0 :(得分:2)
您可以在HTML中的提交按钮下方添加一个空段落:
<p id="message"></p>
然后在您的脚本中设置段落的内容 - 这将需要在fsubmit
函数内部,以便在用户单击按钮时执行:
var message = "Bad luck";
if (score >= 7) {
message = "Great job!";
}
document.getElementById("message").innerHTML = message;
答案 1 :(得分:0)
<script>
var messages = {
6: "Bad luck",
7: "Great job!"
}
document.getElementById("message").innerHTML = messages[score];
</script>