我正在构建一个使用单选按钮(在3个单独的html页面上)具有一些测验问题的应用程序。 我通过将页面标题带到变量中来编写了一个适合所有3个html页面的js函数(thisquestion)。然后,我将用户的单选按钮响应(选项一,选项二,选项三或选项四)拉到变量(selectedValue)中。
我正在针对预期的正确答案测试此问题和selectedValue的所有可能组合(answerFlags是简单的二进制变量:正确= 1,不正确= 2)
我正在努力从哪里开始!我是茉莉的新手。
js函数是...
//Function: check the answers against desired for each question-xxx.html //
function checkQuestionRadio() {
const rbs = document.querySelectorAll('input[name="question"]');
let selectedValue;
for (const rb of rbs) {
if (rb.checked) {
selectedValue = rb.id;
break;
}
}
// log to Console to test Functionality //
console.log(selectedValue);
let thisquestion = document.title;
// answers to radio button style questions //
switch(thisquestion) {
case 'Online Learning - Question 1':
if (selectedValue == 'optionfour') {
answerFlagOne = 1;
} else {
answerFlagOne = 0;
}
break;
case 'Online Learning - Question 3':
if (selectedValue == 'optiontwo') {
answerFlagThree = 1;
} else {
answerFlagThree = 0;
}
break;
case 'Online Learning - Question 8':
if (selectedValue == 'optionthree') {
answerFlagEight = 1;
} else {
answerFlagEight = 0;
}
break;
default:
console.log('did not work');
break;
}
// log to Console to test Functionality //
console.log('answerFlagOne: ' + answerFlagOne);
console.log('answerFlagThree: ' + answerFlagThree);
console.log('answerFlagEight: ' + answerFlagEight);
}