我将使用JavaScript来创建和评分测验。它有20个问题,并随机挑选其中10个作为测验。我需要帮助的是在下面的真实长行中找到。看看它说id =“q +正确”?此ID将用于对测验进行评分以查看是否进行了检查。变量“q”包含为测验随机选择的数字(从1到10)。例如:如果q = 1(它在测验中首先被选中),我将使用document.getElementById('1correct').checked
来查看它是否被选中。但是,我不知道如何在此上下文中将变量“q”连接到字符串“correct”。我会很感激任何答案。如何将字符串添加到<input>标记中的变量?
function choosequestions(){
var qList = [];
for(q=1; q < 11; q++){
var question = Math.floor((Math.random()*20)+1);
while (qList.indexOf(question) !== -1) {
question = Math.floor((Math.random()*20)+1);
}
qList.push(question);
if (question==1){
document.getElementById(q).innerHTML='Question '+ q +'. Which of these freedoms are protected in the 1st amendment?<br /><br /><input id="q + correct" type="radio" name="fav" value="country" />Freedoms of Religion, Speech and Petition.<br /> <input id="q + .2" type="radio" name="fav" value="blues" />Right to bear arms.<br /> <input id="q + .3" type="radio" name="fav" value="rock" />Right to \"Life, liberty, and the pursuit of happiness.\"<br /> <input id="q + .4" type="radio" name="fav" value="pop" />All of the above.</form>'
}
}
}
我有点麻烦,我的功能,检查哪个无线电框被选中。你能帮助@TyrannicalTyrannosaurus吗? JS可以在这里找到:http://js.do/ianhazzard/31112 –
太棒了! Fo sho - 它看起来像在第二个输入(第8行)中错过了'onClick'的一个'''标记:D –