积极答复者
在IE8中,如果一个页面上包含多相名称相同的的Html Element的取值问题

问题
-
没有找到专门用于讨论IE8的版块,只好发现在这个地方了。
在IE8中,如果一个页面包含多个名称/id相同的Html Element,如:一个页面上包含有三个名称为"test"的radio,如果我想知道第一个radio是否被选中,在以前的IE版本中可以通用以下代码实现:document.all("test").checded, 但是在IE8中却不能这样做??
是何原因,如果IE8中已不支持这种方式,那么有没有其它替代的解决办法?
环境:
1. Window xp sp2, IE8 b2
2. Window server 2003 sp2, IE8 b2
示例代码如下:Code Snippet<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>测试IE8.0</title>
</head><script language="javascript" type="text/javascript">
function CheckedIndex(radioObject,index)
{
try{
var obj=document.all(radioObject)[index];
alert("the type of radioObject:\t"+typeof(document.all(radioObject))+"\nthe type of obj:\t"+typeof(obj));
if(obj.checked)
{
obj.checked=false;
}
else
{
obj.checked=true;
}
}
catch(e){
alert("error message:"+e.message);
}
}
function CheckOp()
{
document.all("spanResult").innerHTML="start...";
try{
var obj = document.all("test");
var strResult="";
for(var i=0;i<obj.length;i++){
if(obj[ i ].checked)
strResult+="<li>Index "+i+" checked</li>";
else
strResult+="<li>Index "+i+" not checked</li>";
}
document.all("spanResult").innerHTML=strResult;
}
catch(e){
alert("error message:"+e.message);
}
}
</script><body>
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td width="100%" height="30" colspan="2" bgcolor="#cccccc">
1. 1926年英国工程师约翰。贝德 制造出世界第一台简易电视机,1950年彩色电视机研制成功。我国中央电视台的前身北京电视台于什么时间开始试播,并于当年播同、出我国第一部电视剧《一口菜饼子》</td>
</tr>
<tr bgcolor='#ffffff'>
<td width='10px' height='30px' nowrap>
</td>
<td width='100%'>
<input type='radio' id='test' name='test' value='A'>A.<span onclick="CheckedIndex('test',0)">1958年1月1日</span>
</td>
</tr>
<tr bgcolor='#ffffff'>
<td width='10px' height='30px' nowrap>
</td>
<td width='100%'>
<input type='radio' id='test' name='test' value='B'>B.<span onclick="CheckedIndex('test',1)">1958年5月1日</span>
</td>
</tr>
<tr bgcolor='#ffffff'>
<td width='10px' height='30px' nowrap>
</td>
<td width='100%'>
<input type='radio' id='test' name='test' value='C'>C.<span onclick="CheckedIndex('test',2)">1958年10月1日</span>
</td>
</tr>
<tr bgcolor='#ffffff'>
<td height='30px' nowrap>
</td>
<td>
<input type="button" name="btnCheckOp" value="检查选项" onclick="BLOCKED SCRIPTCheckOp();" /></td>
</tr>
<tr bgcolor='#ffffff'>
<td height='30px' nowrap>
</td>
<td>
<span id="spanResult"></span>
</td>
</tr>
</table>
</body>
</html>