注册 登录
编程论坛 ASP技术论坛

为什么单选按钮 document。getelementbyid()取值会有问题红色部分

a124211741 发布于 2011-03-24 11:53, 1178 次点击
程序代码:
<input name="option" type="radio" value="1" checked="checked"/>
<input type="radio" name="option" value="2" />
<input type="radio" name="option" value="3" />
<input name="option1" type="radio" value="1" />      
<input type="radio" name="option2" value="2" />
<input type="radio" name="option3" value="3" />
<script language="vbscript">
function pk()
document.getElementById("option1").checked = false
document.getElementById("option2").checked = false
document.getElementById("option3").checked = false
dim x,y,a,w
randomize
x = int(rnd()*3)+1
select case x
case 1
document.getElementById("option1").checked = true
case 2
document.getElementById("option2").checked = true
case 3
document.getElementById("option3").checked = true
end select
if document.getElementById("option").value =1 and document.getElementById("option").checked = true then
    y=1
elseif document.getElementById("option").value =1 and document.getElementById("option").checked = true then
    y=2
else
    y=3
end if
if  x - y = -1 or  x - y = 2  then
msgbox "恭喜你,你赢了 "
elseif x - y = 0 then
msgbox "打平手"
else
msgbox "电脑胜出"
end if
end function
</script>
6 回复
#2
a1242117412011-03-24 11:54
问题补充:
   
程序代码:
if document.getElementById("option").value =1 and document.getElementById("option").checked = true then
    y=1
elseif document.getElementById("option").value =1 and document.getElementById("option").checked = true then
    y=2
else
    y=3
end if
在这段代码的中间那段elseif 这段没有被执行 ,为什么?求解,求高人指点
#3
gupiao1752011-03-24 14:02
<input type="radio" name="option" value="2" />
document.getElementById("option")是通过ID来获取对象的,而你的标签里根本没有ID!必须把所有标签改为:
<input type="radio" name="option" value="2" id="option1"/>
注意:所有标签的id必须唯一,不能重复!
#4
hams2011-03-24 15:37
前面 if这段的条件和elseif这段的条件是完全一样的,前面执行了后面当然不会执行。
#5
a1242117412011-03-24 17:15
程序代码:
if document.getElementById("option").value =1 and document.getElementById("option").checked = true then
    y=1
elseif document.getElementById("option").value =2 and document.getElementById("option").checked = true then
    y=2
else
    y=3
end if
但是就算改成这样的结果elseif语句还是完全没有被执行啊
#6
hams2011-03-24 17:18
条件不符当然不执行
#7
a1242117412011-03-24 17:24
我将option.value = 2  的控件 单机,也就是说符合条件elseif了 ,可是运行的时候只有 value 1 和 3 被执行 ,就算我选到了value = 2 的单选按钮 他也是直接执行 else 语句,完全忽略了elseif语句
1