我在JavaScript中进行测验,从20个列表中随机选择10个问题。我的代码说:
if (question==1){
document.getElementById(q).innerHTML="Question 1"
}
我的问题是我的最后一次测验不会说“问题1”。我希望它能为多项选择编写单选按钮。如何用单选按钮替换“问题1”?
另外,我不希望我的代码多次提出相同的问题。例如:所以他们两次不回答问题4。这是我的完整代码:
<body>
<input type="button" value="Start!" onclick="choosequestions()"/><br><br>
<div id="1"></div><br>
<div id="2"></div><br>
<div id="3"></div><br>
<div id="4"></div><br>
<div id="5"></div><br>
<div id="6"></div><br>
<div id="7"></div><br>
<div id="8"></div><br>
<div id="9"></div><br>
<div id="10"></div><br>
<body>
<script>
function choosequestions(){
for(q=1;q < 11;q++){
var question=Math.floor((Math.random()*20)+1)
if (question==1){
document.getElementById(q).innerHTML="Question 1"
}else if (question==2){
document.getElementById(q).innerHTML="Question 2"
}else if (question==3){
document.getElementById(q).innerHTML="Question 3"
}else if (question==4){
document.getElementById(q).innerHTML="Question 4"
}else if (question==5){
document.getElementById(q).innerHTML="Question 5"
}else if (question==6){
document.getElementById(q).innerHTML="Question 6"
}else if (question==7){
document.getElementById(q).innerHTML="Question 7"
}else if (question==8){
document.getElementById(q).innerHTML="Question 8"
}else if (question==9){
document.getElementById(q).innerHTML="Question 9"
}else if (question==10){
document.getElementById(q).innerHTML="Question 10"
}else if (question==11){
document.getElementById(q).innerHTML="Question 11"
}else if (question==12){
document.getElementById(q).innerHTML="Question 12"
}else if (question==13){
document.getElementById(q).innerHTML="Question 13"
}else if (question==14){
document.getElementById(q).innerHTML="Question 14"
}else if (question==15){
document.getElementById(q).innerHTML="Question 15"
}else if (question==16){
document.getElementById(q).innerHTML="Question 16"
}else if (question==17){
document.getElementById(q).innerHTML="Question 17"
}else if (question==18){
document.getElementById(q).innerHTML="Question 18"
}else if (question==19){
document.getElementById(q).innerHTML="Question 19"
}else if (question==20){
document.getElementById(q).innerHTML="Question 20"
}
}
}
</script>
答案 0 :(得分:0)
首先,您可以将功能简化为:
function choosequestions(){
for(q=1; q < 11; q++){
var question=Math.floor((Math.random()*20)+1);
document.getElementById(q).innerHTML = 'Question ' + question;
}
}
其次,为了确保您没有多次选择相同的问题,请执行以下操作:
function choosequestions(){
var qList = [];
for(q=1; q < 11; q++){
var question = Math.floor((Math.random()*20)+1);
// Check to make sure Question # 'question' has not been added already:
while (qList.indexOf(question) !== -1) {
question = Math.floor((Math.random()*20)+1);
}
qList.push(question);
document.getElementById(q).innerHTML = 'Question ' + question;
}
}
如果你不理解这个功能,请告诉我,我可以解释一下。
最后,要使文本进入单选按钮,并使用上面的功能,请更改为:
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);
// Create radio button within each div element:
document.getElementById(q).innerHTML = '<input type="radio" name="question" value="' + q + '">Question ' + question;
}
}
希望这有用!
修改:根据您的意见,我相信这就是您所寻找的:
function choosequestions(){
var qList = [];
for(q=1; q < 11; q++){
var question = Math.floor((Math.random()*20)+1);
var message = '';
while (qList.indexOf(question) !== -1) {
question = Math.floor((Math.random()*20)+1);
}
qList.push(question);
// Create radio button within each div element:
if (question==1){
message = "Question 1";
}else if (question==2){
message = "Question 2";
}
...
// Lots of other 'else if' statements
...
}else if (question==20){
message = "Question 20";
}
document.getElementById(q).innerHTML = message;
}
}
这样,您仍然可以将html抛出到每个特定问题div中。