<form method="post" action="javascript:alert('Form submitted!')" id="myForm">
<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>
<script type="text/javascript">
(function(){
var form = document.getElementById("myForm");
var field1=form.elements[0];
取得表单的第一个字段
var field2=form.elements["textbox2"];
取得表单名为“textbox2”的字段
字段数量
var fieldcount=form.elements.length;
var colorFields = form.elements["color"];
alert(colorFields.length); //3
var firstColorField = colorFields[0];
var firstFormField = form.elements[0];
alert(firstColorField === firstFormField); //true
})();
</script>
也可以通过form[0]或者form["color"]来读取,但不推荐