[英]JavaScript multiple choice quiz. Correct answers display element
function HideSectionTwo() {
// If correct answers are checked
if(document.getElementById("S1Q1A").checked == true) {
document.getElementById("sectiontwo").style.display = "";
}
//else if nothing is checked
else {
document.getElementById("sectiontwo").style.display = "none";
}
}
我是JavaScript的新手,但我正在尝试创建一个多选题测验,每个部分有4个问题。 一旦在该部分中正确回答了所有答案,那么它将显示下一部分。 对于我的HTML,我正在使用表,并将整个部分存储在这些单独的表中,其id标记为“sectionone”,“sectiontwo”等等。 上面的代码是我一直在努力的。 我尝试了getElementByClass并按类分配了我的元素名称,但它没有用。 我只是陷入困境,需要了解哪个方向。
我的脚本适用于选择单个答案。 如果我选择第1节问题1答案A将显示ID为“sectiontwo”的表格。
您可以尝试使用一个变量来跟踪一个部分中填充的问题数量,然后检查该变量以决定是否显示下一部分。 就像是:
//count of questions answered
var s1count = 0;
//question 1
if (document.getElementById('S1Q1A').checked || document.getElementById('S1Q1B').checked || document.getElementById('S1Q1C').checked || document.getElementById('S1Q1D').checked) {
s1count++;
}
//question 2
if (document.getElementById('S1Q2A').checked || document.getElementById('S1Q2B').checked || document.getElementById('S1Q2C').checked || document.getElementById('S1Q2D').checked) {
s1count++;
}
//and so on...
//check the count, say if section 1 has 5 questions
if (s1count == 5) {
document.getElementById("sectiontwo").style.display = "block"; //or inline
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.