js如何赋值变量给id?
<html><head><scripttype=text/javascript>varquiz=["gay?<br/><inputtype='radio'name='ga...
<html>
<head>
<script type=text/javascript>
var quiz=["gay?<br/> <input type='radio' name='ga' value=y>yes <input type='radio' name='ga' value=n>no<br/>",
"lesbian?<br/> <input type='radio' name='le' value=y>yes <input type='radio' name='le' value=n>no<br/>",
"sex?<br/> <input type='radio' name='se' value=y>male <input type='radio' name='se'value=n>female<br/>"]
var f=document.getElementsByName("ga");
var s=document.getElementsByName("le");
var t=document.getElementsByName("se");
function checkanswer(){
if(f.item[0].checked==true){document.getElementById(a).innerText="correct";}
else document.getElementById(a).innerText="the answer should be yes";
if(s.item[0].checked==true){document.getElementById(b).innerText="correct";}
else document.getElementById(b).innerText="the answer should be yes";
if(t.item[0].checked==true){document.getElementById(a).innerText="correct";}
else document.getElementById(a).innerText="the answer should be male";
}
</script>
</head>
<body>
<script>
var a=parseInt(Math.floor( Math.random() * 3 ));
var b=parseInt(Math.floor(Math.random()*3));
while(a==b){
var a=Math.floor( Math.random() * 3 );
var b=Math.floor(Math.random()*3);}
document.write("1."+quiz[a]+"<br/>");
document.write("<div id=a></div>")
document.write("2."+quiz[b]+"<br/>");
document.write("<div id=b></div>")
</script>
<input type="button" name="submit" value="Submit" onclick="checkanswer()">
</body>
</html>主要目的就是检测题目答案是否正确,并在各题下面显示正确答案
可以显示出题目,但是检测答案的function无效。。。。大神们,能帮看看是哪里有问题吗? 展开
<head>
<script type=text/javascript>
var quiz=["gay?<br/> <input type='radio' name='ga' value=y>yes <input type='radio' name='ga' value=n>no<br/>",
"lesbian?<br/> <input type='radio' name='le' value=y>yes <input type='radio' name='le' value=n>no<br/>",
"sex?<br/> <input type='radio' name='se' value=y>male <input type='radio' name='se'value=n>female<br/>"]
var f=document.getElementsByName("ga");
var s=document.getElementsByName("le");
var t=document.getElementsByName("se");
function checkanswer(){
if(f.item[0].checked==true){document.getElementById(a).innerText="correct";}
else document.getElementById(a).innerText="the answer should be yes";
if(s.item[0].checked==true){document.getElementById(b).innerText="correct";}
else document.getElementById(b).innerText="the answer should be yes";
if(t.item[0].checked==true){document.getElementById(a).innerText="correct";}
else document.getElementById(a).innerText="the answer should be male";
}
</script>
</head>
<body>
<script>
var a=parseInt(Math.floor( Math.random() * 3 ));
var b=parseInt(Math.floor(Math.random()*3));
while(a==b){
var a=Math.floor( Math.random() * 3 );
var b=Math.floor(Math.random()*3);}
document.write("1."+quiz[a]+"<br/>");
document.write("<div id=a></div>")
document.write("2."+quiz[b]+"<br/>");
document.write("<div id=b></div>")
</script>
<input type="button" name="submit" value="Submit" onclick="checkanswer()">
</body>
</html>主要目的就是检测题目答案是否正确,并在各题下面显示正确答案
可以显示出题目,但是检测答案的function无效。。。。大神们,能帮看看是哪里有问题吗? 展开
1个回答
展开全部
<html>
<head>
<title>1</title>
<script type=text/javascript>
var quiz = ["gay?<br/> <input type='radio' name='ga' value=y>yes <input type='radio' name='ga' value=n>no<br/>",
"lesbian?<br/> <input type='radio' name='le' value=y>yes <input type='radio' name='le' value=n>no<br/>",
"sex?<br/> <input type='radio' name='se' value=y>male <input type='radio' name='se'value=n>female<br/>"]
var f = document.getElementsByName("ga");
var s = document.getElementsByName("le");
var t = document.getElementsByName("se");
function checkanswer() {
if (f.length) {
if ((f[0].checked == true)) {
document.getElementById("a").innerText = "correct";
}
else document.getElementById("a").innerText = "the answer should be yes";
}
if (s.length) {
if ((s[0].checked == true)) {
document.getElementById("b").innerText = "correct";
}
else document.getElementById("b").innerText = "the answer should be yes";
}
if (t.length) {
if ((t[0].checked == true)) {
document.getElementById("a").innerText = "correct";
}
else document.getElementById("a").innerText = "the answer should be male";
}
}
</script>
</head>
<body>
<script>
var a = parseInt(Math.floor(Math.random() * 3));
var b = parseInt(Math.floor(Math.random() * 3));
while (a == b) {
var a = Math.floor(Math.random() * 3);
var b = Math.floor(Math.random() * 3);
}
document.write("1." + quiz[a] + "<br/>");
document.write("<div id='a'></div>")
document.write("2." + quiz[b] + "<br/>");
document.write("<div id='b'></div>")
</script>
<input type="button" name="submit" value="Submit" onclick="checkanswer()">
</body>
</html>
追问
可以显示答案提示,谢谢啦,只是id可不可以是变量,我是随机产生2道题,div也对应,然后检测答案显示可不可以显示在对应的div区域里?想a和b是0~2之间的整数,那function可不可以对应 document.getElementById("0").innerText, document.getElementById("1").innerText。。。?
追答
id 可以是变量
不过变量的命名要符合规则
直接用数字是不行的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询