【发布时间】:2017-05-04 18:39:24
【问题描述】:
我是编程新手。我目前面临的问题是无法显示复选框问题的正确答案。题号为 4,例如选择题 4 将不会显示正确答案 River Thames。确保第 4 题是正确的。
Javascript:
myTimer = setTimeout('alert("Time out")', 600000); /*alert will appear after 60 second*/
function processMyForm() {
var chosenans1 = document.mainForm.answer1.value;
var a1;
//alert(document.mainForm.answer1.value);
//alert(document.getElementById('answer2'));
var chosenans2 = document.getElementById('answer2').value;
var a2;
var chosenans4 = document.mainForm.answer4.checked;
var a4;
// ----------------------------
var total;
// --------------------------------
if (chosenans1 == "England") /*if radio value of answer1 is "England"*/ {
a1 = 2; /*correct answer for 2 marks*/
document.getElementById("result1").innerHTML = "<span class='correct'>Your answer to question 1 is correct.</span> "; /*print out correct at bottom of the page at <div id = "result1">*/
} else {
a1 = -1; /*incorrect answer for -1 mark*/
document.getElementById("result1").innerHTML = "<span class='incorrect'>The correct answer to question 1 is 'England'.</span>"; /*print out incorrect at <div id = "result1">*/
}
// ---------------------
if (chosenans2 == "Stonehenge") {
a2 = 2;
document.getElementById("result2").innerHTML = "<span class='correct'>Your answer to question 2 is correct.</span>";
} else {
a2 = -1;
document.getElementById("result2").innerHTML = "<span class='incorrect'>The correct answer to question 2 is 'Stonehenge'.</span>";
}
// -------------------
if (chosenans3 == "2012") {
a3 = 2;
document.getElementById("result3").innerHTML = "<span class='correct'>Your answer to question 3 is correct.</span>";
} else {
a3 = -1;
document.getElementById("result3").innerHTML = "<span class='incorrect'>The correct answer to question 3 is '2012'.</span>";
}
// ---------------------
if (chosenans4 == "River Thames") {
a4 = 2;
document.getElementById("result4").innerHTML = "<span class='correct'>Your answer to question 4 is correct.</span>";
} else {
a4 = -1;
document.getElementById("result4").innerHTML = "<span class='incorrect'>The correct answer to question 4 is 'River Thames'.</span>";
}
// --------------------------------------------------------
total = a1 + a2 + a3 + a4 ;//+ a5 + a6; /*add marks(2 or -1) together*/
document.getElementById("result").innerHTML = ("Your mark is " + total); //print out your total mark at <div id = "result">
alert("Your mark is " + total); //prompt total mark in small window
if (total < 4) {
document.body.style.backgroundImage = "none"; //remove background image
document.body.style.backgroundColor = "#bb0000"; //add a background colour
} else {
document.body.style.backgroundImage = "none";
document.body.style.backgroundColor = "#006600";
}
clearTimeout(myTimer); //stop timer
}
function getValue(qArray) { //get value from radio array
var i;
for (i = 0; i < qArray.length; i++) {
if (qArray[i].checked) return qArray[i].value;
}
return "";
}
HTML:
<form id="mainForm" name="mainForm">
<p>1: London belongs to:</p><br>
<input type="radio" name="answer1" value="England">England<br/>
<input type="radio" name="answer1" value="Scotland">Scotland<br/>
<input type="radio" name="answer1" value="Wales">Wales<br/>
<input type="radio" name="answer1" value="Northern Ireland">Northern Ireland<br/>
<!--radio buttons with different values-->
<br>
<p>2: Which of the following is not in London:</p><br>
<select id="answer2">
<option value="">Select an answer</option>
<option value="St Paul's Cathedral">St Paul's Cathedral</option>
<option value="Buckingham Palace">Buckingham Palace</option>
<option value="Stonehenge">Stonehenge</option>
</select>
<br><br>
<br>
<p>4: Which river runs through London:</p>
<br>
<input type="checkbox" name="answer4" value="La Seine">La Seine<br>
<input type="checkbox" name="answer4" value="Rhine River">Rhine River<br>
<input type="checkbox" name="answer4" value="River Thames">River Thames<br>
<br>
<input type="button" onclick="processMyForm()" value="Check answers">
<!--a button called "Check answers" will run processMyForm procedure when user click-->
</form>
<!--print out user's total mark-->
<br/>
<div id="result1"></div>
<div id="result2"></div>
<div id="result3"></div>
<div id="result4"></div>
<div id="result"></div>
【问题讨论】:
-
通过验证,您是要确保他们已回答还是要确保答案正确?通过在同一页面上包含您的答案,知识渊博的用户将能够查看答案 - 您的验证应该是服务器端的。
-
确保第 4 题是正确的。
-
无验证
-
如果您想允许复选框输入有多个答案,您应该更改名称以包含数组表示法,例如
name="answer4[]" -
请您为我应用此更改
标签: javascript html validation dom