<html>
<head>
<title>form test </title>
<script type="text/javascript">
var form1 = document.getElementById("form1");
var colorFields = form1.elements["color"];
alert(colorFields.length);
var firstColorField = colorFields[0];
var firstFormField = form1.elements[0];
alert(firstColorField == firstFormField);
</script>
</head>
<body>
<form method="post" id="form1">
<ul>
<li><input type="radio" name="color" value="red">Red</li>
<li><input type="radio" name="color" value="green">Green</li>
<li><input type="radio" name="color" value="blue">Blue</li>
</ul>
</form>
</body>
</html>
------解决方案--------------------
<html>
<head>
<title>form test </title>
<script type="text/javascript">
function init(){
var form1 = document.getElementById("form1");
var colorFields = form1.elements["color"];
alert(colorFields.length);
var firstColorField = colorFields[0];
var firstFormField = form1.elements[0];
alert(firstColorField == firstFormField);
}
window.onload=init;
</script>
</head>
<body>
<form method="post" id="form1">
<ul>
<li><input type="radio" name="color" value="red">Red</li>
<li><input type="radio" name="color" value="green">Green</li>
<li><input type="radio" name="color" value="blue">Blue</li>
</ul>
</form>
</body>
</html>
这样试试 加载是从上而下进行的(一般的说),你的var form1 = document.getElementById("form1");
时其实form1还没加载到呢 所以找不到对象才对 不止是不是你要的
------解决方案--------------------
------解决方案--------------------
养成好习惯 window.onload 要写 不然DOM树还没加载完 就开始调用它 怎么可能会有对象