1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> 5 <title>form操作</title> 6 <script type="text/javascript" src="test01.js"></script> 7 </head> 8 <body> 9 <form method="post" id="myForm"> 10 <ul> 11 <li><input type="radio" name="color" value="red" />Red</li> 12 <li><input type="radio" name="color" value="green" />Green</li> 13 <li><input type="text" name="color" value="blue" />Blue</li> 14 </ul> 15 </form> 16 </body> 17 </html>
1 window.onload = function() { 2 var form = document.getElementById('myForm'); 3 var field = form.elements[0]; 4 5 //修改value属性 6 field.value = "Another value"; 7 console.log("field.value: " + field.value); 8 9 //检查form属性的值 10 console.log(field.form === form); 11 12 //把焦点设置到当前字段 13 form.elements[2].focus(); 14 15 //禁用当前字段 16 form.elements[1].disabled = true; 17 18 //修改type属性 19 field.type = "checkbox"; 20 };