我試圖在HTML表單標籤中使用簡單函數和單選按鈕創建單選題測驗,但它只允許我爲整個測驗選中一個單選按鈕每個問題一個。單選題測驗 - 單選按鈕和功能問題
此外,提交按鈕沒有激活功能checkAll
,我不知道這是否是因爲我試圖做一些不可能或簡單的事情,因爲我只是錯過了一些東西。任何幫助,將不勝感激!
代碼如下。 。 。我只對JavasScript和html以及StackOverflow感興趣,如果我在這個問題上做錯了任何事情,我都會很抱歉。
<head>
<script language="javascript">
var score=0;
function checkAll() {
function questioncheckOne(){
var correctAnswer = document.getElementById("a3")
if (correctAnswer.checked == true) {
score++;
alert("Correct, your score is now " +score)
}
else {
alert("Wrong, your score is now " +score)
}
};
function questioncheckTwo(){
var correctAnswer = document.getElementById("b2")
if (correctAnswer.checked == true) {
score++;
alert("Correct, your score is now " +score)
}
else {
alert("Wrong, your score is now " +score)
}
};
function questioncheckThree(){
var correctAnswer = document.getElementById("c4")
if (correctAnswer.checked == true) {
score++;
alert("Correct, your score is now " +score)
}
else {
alert("Wrong, your score is now " +score)
}
};
function questioncheckFour(){
var correctAnswer = document.getElementById("d3")
if (correctAnswer.checked == true) {
score++;
alert("Correct, your score is now " +score)
}
else {
alert("Wrong, your score is now " +score)
}
};
function questioncheckFive(){
var correctAnswer = document.getElementById("e3")
if (correctAnswer.checked == true) {
score++;
alert("Correct, your score is now " +score)
}
else {
alert("Wrong, your score is now " +score)
}
};
};
</script>
</head>
<body>
<form name ="Where in the world is...?">
<p>
Question1: Where in the world would you find the Spire?
</p>
<input type="radio" name="radio" id="a1" value="a1" /> Kerry. </input>
<input type="radio" name="radio" id="a2" value="a1" /> Galway. </input>
<input type="radio" name="radio" id="a3" value="a1" /> Dublin. </input>
<input type="radio" name="radio" id="a4" value="a1" /> Donegal. </input>
<p>
Question2: Where in the world would you find the Colosseum?
</p>
<input type="radio" name="radio" id="b1" value="a2" /> Taipei. </input>
<input type="radio" name="radio" id="b2" value="a2" /> Rome. </input>
<input type="radio" name="radio" id="b3" value="a2" /> Reykjavic. </input>
<input type="radio" name="radio" id="b4" value="a2" /> Brussels. </input>
<p>
Question3: Where in the world would you find the Taj Mahal?
</p>
<input type="radio" name="radio" id="c1" value="a3" /> London. </input>
<input type="radio" name="radio" id="c2" value="a3" /> Brisbane. </input>
<input type="radio" name="radio" id="c3" value="a3" /> Paris. </input>
<input type="radio" name="radio" id="c4" value="a3" /> Agra. </input>
<p>
Question4: Where in the world would you find the Parthenon?
</p>
<input type="radio" name="radio" id="d1" value="a4" /> Edinburgh. </input>
<input type="radio" name="radio" id="d2" value="a4" /> Oslo. </input>
<input type="radio" name="radio" id="d3" value="a4" /> Athens. </input>
<input type="radio" name="radio" id="d4" value="a4" /> Pyongyang. </input>
<p>
Question5: Where in the world would you find the Niagara Falls?
</p>
<input type="radio" name="radio" id="e1" value="a5" /> Hong Kong. </input>
<input type="radio" name="radio" id="e2" value="a5" /> Moscow. </input>
<input type="radio" name="radio" id="e3" value="a5" /> New York. </input>
<input type="radio" name="radio" id="e4" value="a5" /> Ottawa. </input>
<p>
<input type="button" name="submit" id="submit" value="submit" onclick="checkAll()" /> </input>
</p>
</form>
</body>
</html>
您的'checkAll()'函數正在運行,但您實際上不會調用您的其他支票函數內部;你只是宣佈他們。所以看起來似乎沒有任何東西在運行。您還需要防止默認提交操作。一種方法是在該函數結束時返回「false」。 – Interrobang 2014-11-02 02:05:38
你可以把這個減少到最小(非)工作的例子嗎?並且使用變量而不是一遍又一遍地複製函數。 – 2014-11-02 02:12:23
我應該在checkAll函數內的每個函數的末尾返回false?使用變量意味着我不得不使用數組,我知道它會更加高效,但不知道如何編寫它。 – 2014-11-02 02:15:25