我现在已经参加了这个测验多年了,而且无法让它在我的网页上工作。错误是什么?
HTML:
<body>
<div id="content1">
<div class="position">
<div class="form">
<h3>Quiz</h3>
<form name="myForm">
Question 1: Which of these is a letter?<br>
<input type="radio" name="q1" value="correct">A<br>
<input type="radio" name="q1">1<br>
<input type="radio" name="q1">#<br>
<input type="radio" name="q1">None of the Above<br>
<br>
Question 2: Which of these is a number?<br>
<input type="radio" name="q2">A<br>
<input type="radio" name="q2" value="correct">1<br>
<input type="radio" name="q2">#<br>
<input type="radio" name="q2">None of the Above<br>
<br>
etc...
<input name="button" type="Submit" onClick="onclick=return checkAnswers()" />
</form>
</div>
</div>
</div>
</body>
JavaScript代码:
<head>
<script language="JavaScript">
//Put all the question sets into this array.
var allQuestions = new Array(document.myForm.q1,
document.myForm.q2,
document.myForm.q3,
document.myForm.q4);
//Redirects if 75% or greater, returns false otherwise.
function checkAnswers(){
var totalScore = 0; //initialize to 0
//Go through each question set.
for (var i in allQuestions) {
var temp = allQuestions[i];
//Go through each radio button in the current question set.
for (var j = 0; j < temp.length; j++) {
//If the correct one is chosen then add 1 to total score.
if (temp[j].value == "correct" && temp[j].checked == true) {
totalScore++;
}
}
}
//If the total percentage is more than 75%.
if ((totalScore/allQuestions.length) >= .75) {
//Alert and move on.
alert("Congratulations! Your score of " + totalScore +
" out of " + allQuestions.length + " is good enough to proceed!");
else{
//Otherwise alert and return false.
alert("You must get at least 75% correct to move on!");
return false;
}
}
</script>
</head>
答案 0 :(得分:5)
您可以在表单元素存在之前访问它们 你错过了其他的大括号。我把它消灭了。
<script type="text/javascript>
function validate(theForm) {
//put all the question sets into this array
var allQuestions = new Array(theForm.q1,
theForm.q2 /*,
theForm.q3,
theForm.q4 */);
return checkAnswers(allQuestions);
}
//redirects if 75% or greater, returns false otherwise
function checkAnswers(allQuestions){
var totalScore = 0; //initialize to 0
//go through each question set
for (var i in allQuestions) {
var temp = allQuestions[i];
//go through each radio button in the current question set
for (var j = 0; j < temp.length; j++) {
//if the correct one is chosen then add 1 to total score
if (temp[j].value == "correct" && temp[j].checked == true) {
totalScore++;
}
}
}
//if the total percentage is more than 75%
if ((totalScore/allQuestions.length) >= .75) {
//alert and move on
alert("Congratulations! Your score of " + totalScore +
" out of " + allQuestions.length + " is good enough to proceed!");
return true; // this will submit the form. return false if you do not want to submit at all
}
//otherwise alert and return false
alert("You must get at least 75% correct to move on!");
return false;
}
</script>
<h3>Wine Quiz</h3>
<form name="Quiz" onsubmit="return validate(this)">
.
.
<input type="submit" />
</form>
答案 1 :(得分:0)
这是正在寻找它的人的工作解决方案和评论!
// JavaScript Document
function validate(theForm) {
//put the questions into an array
var allQuestions = new Array(theForm.q1,
theForm.q2,
theForm.q3,
theForm.q4);
return checkAnswers(allQuestions);
}
//checks all answers
function checkAnswers(allQuestions){
var totalScore = 0; //initialize to 0
//go through each question set
for (var i in allQuestions) {
var temp = allQuestions[i];
//go through each radio button in the current question set
for (var j = 0; j < temp.length; j++) {
//if the correct one is chosen then add 1 to total score
if (temp[j].value == "correct" && temp[j].checked == true) {
totalScore++;
}
}
}
//if the total percentage is more than 75%
if ((totalScore/allQuestions.length) >= .75) {
//alert and move on
alert("Congratulations! Your score of " + totalScore +
" out of " + allQuestions.length + " is very good!");
return true;
}
//otherwise alert and return false
alert("You must get at least 75% correct to pass");
return false;
}