我正在尝试自学代码,目前我正忙于我正在从事的一个迷你项目。我相信我已经完成了 HTML 代码;但是,我很难让 Javascript/jQuery 做我想做的事情。
我希望 Javascript/jQuery 对我的测验进行评分并将分数打印在不同的页面上。我不确定我的语法是否错误,或者我是否打错电话或......?
这是HTML:
<body>
<div class="center">
<div id="questionContainer">
<div id="header">
<h1>State Capitol Quiz</h1>
</div>
<div id="left">
<form action="quiz.html" class="container">
<div class="blank">
<br />
<br />
<p id="one">1. What is the capitol of Michigan?<br />
<div class="tab">
<input type="radio" name="question" value="A" /> Lansing<br />
<input type="radio" name="question" value="B" /> Richmond<br />
<input type="radio" name="question" value="C" /> Austin<br />
<input type="radio" name="question" value="D" /> Columbus<br />
</div>
</p>
</div>
</form>
<form action="quiz.html" class="container">
<div class="blank">
<br />
<br />
<p id="two">2. What is the capitol of Virginia?<br />
<div class="tab">
<input type="radio" name="question" value="A" /> Lansing<br />
<input type="radio" name="question" value="B" /> Richmond<br />
<input type="radio" name="question" value="C" /> Lincoln<br />
<input type="radio" name="question" value="D" /> Reno<br />
</div>
</p>
</div>
</form>
<form action="quiz.html" class="container">
<div class="blank">
<br />
<br />
<p id="three">3. What is the capitol of Arkansas?<br />
<div class="tab">
<input type="radio" name="question" value="A" /> Dallas<br />
<input type="radio" name="question" value="B" /> Lincoln<br />
<input type="radio" name="question" value="C" /> Little Rock<br />
<input type="radio" name="question" value="D" /> Cheyene<br />
</div>
</p>
</div>
</form>
<form action="quiz.html" class="container">
<div class="blank">
<br />
<br />
<p id="four">4. What is the capitol of Arizona?<br />
<div class="tab">
<input type="radio" name="question" value="A" /> Juneau<br />
<input type="radio" name="question" value="B" /> Honolulu<br />
<input type="radio" name="question" value="C" /> Pheonix<br />
<input type="radio" name="question" value="D" /> Toledo<br />
</div>
</p>
</div>
</form>
<form action="quiz.html" class="container">
<div class="blank">
<br />
<br />
<p id="five">5. What is the capitol of Florida?<br />
<div class="tab">
<input type="radio" name="question" value="A" /> Oakland<br />
<input type="radio" name="question" value="B" /> Kansas City<br />
<input type="radio" name="question" value="C" /> Tallahassee<br />
<input type="radio" name="question" value="D" /> Miami<br />
</div>
</p>
</div>
</form>
<form action="quiz.html" class="container">
<div class="blank">
<br />
<br />
<p id="six">6. What is the capitol of Nebraska?<br />
<div class="tab">
<input type="radio" name="question" value="A" /> Omaha<br />
<input type="radio" name="question" value="B" /> Lincoln<br />
<input type="radio" name="question" value="C" /> Sacramento<br />
<input type="radio" name="question" value="D" /> Sab Diego<br />
</div>
</p>
</div>
</form>
</div>
<div id="right">
<form action="quiz.html" class="container">
<br />
<br />
<div class="blank">
<p id="seven">7. What is the capitol of Texas?<br />
<div class="tab">
<input type="radio" name="question" value="A" /> Fortworth<br />
<input type="radio" name="question" value="B" /> Richmond<br />
<input type="radio" name="question" value="C" /> Austin<br />
<input type="radio" name="question" value="D" /> Las Vegas<br />
</div>
</p>
</div>
</form>
<form action="quiz.html" class="container">
<div class="blank">
<p id="eight">8. What is the capitol of Alaska?<br />
<div class="tab">
<input type="radio" name="question" value="A" /> Tulsa<br />
<input type="radio" name="question" value="B" /> Oaklahoma City<br />
<input type="radio" name="question" value="C" /> Austin<br />
<input type="radio" name="question" value="D" /> Juneau<br />
</div>
</p>
</div>
</form>
<form action="quiz.html" class="container">
<div class="blank">
<p id="nine">9. What is the capitol of Hawaii?<br />
<div class="tab">
<input type="radio" name="question" value="A" /> Paris<br />
<input type="radio" name="question" value="B" /> Honolulu<br />
<input type="radio" name="question" value="C" /> Austin<br />
<input type="radio" name="question" value="D" /> Cadilac<br />
</div>
</p>
</div>
</form>
<form action="quiz.html" class="container">
<div class="blank">
<p id="ten">10. What is the capitol of Ohio?<br />
<div class="tab">
<input type="radio" name="question" value="A" /> Cleveland<br />
<input type="radio" name="question" value="B" /> Green Bay<br />
<input type="radio" name="question" value="C" /> Austin<br />
<input type="radio" name="question" value="D" /> Columbus<br />
</div>
</p>
</div>
</form>
<div id="buttonDiv">
<input type="button" id="button" value="submit" />
</div>
</div>
</div>
</div>
<div class="center">
<div id="answerSheet">
<p id="score"><h1>Your Score!</h1></p>
<br />
<p>Results:</p>
</div>
</div>
</body>
这是Javascript/jQuery:
$(document).ready(function(){
$("#button").click(function(){
$("#questionContainer").fadeOut("slow",function(){
$("#answerSheet").fadeIn("slow",function(){
});
});
var numQues= 10;
var numChoices= 4;
var answers = new Array(10);
answers[0] = "Lansing";
answers[1] = "Richmond";
answers[2] = "Little Rock";
answers[3] = "Pheonix";
answers[4] = "Tallahassee";
answers[5] = "Lincoln";
answers[6] = "Austin";
answers[7] = "Juneau";
answers[8] = "Honolulu";
answers[9] = "Columbus";
function getScore(form){
var score = 0;
var currElt;
var currSelection;
for(i = 0; i<numQues; i++){
currElt = i * numChoi;
for(j = 0; j<numChoi; j++){
currSelection = form.elements[currElt + j];
if(currSelection.checked){
if(currSelection.value == answers[i]){
score++;
break;
}
}
}
}
score = math.round(score/numQues * 100);
form.precentage.value = score + "%";
var corrAnswers = "";
for(i = 1; i <numQues; i++){
corrAnswers += i + "." + answers [i - 1] + "\r\n;"
}
form.solutions.value = corrAnswers/numQues * 100 + "%";}
});
});
谢谢