forms对象集合和form对象
这里我们说明form对象
form对象名.某控件名.
案例
<body>
<form action="a.php" method="post">
u:<input type="text" name="username" value="hello"/><br/>
p:<input type="password" name="password" value="xxx"/><br/>
<input type="submit" value="提交"/>
</form>
<form action="b.php" method="post">
u:<input type="text" name="username"/>
</form>
<input type="button" οnclick="mysubmit()" value="提交"/>
</body>
<script type="text/javascript">
<!--
//window.alert(document.forms.item(1).action);
//取出第一个from function mysubmit(){
var form1=document.forms.item(1);
//window.alert(form1.username.value);
//window.alert(form1.password.value);
form1.submit();
}
//-->
</script>
案例2:验证用户注册
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<script type="text/javascript">
function check(){
//通过表单对象来回去用户的输入
var form = document.forms.item(0);
var name=form.username.value;
var passwd=form.passwd.value;
var passwd2=form.passwd2.value;
//window.alert(name+passwd+passwd2);
if(name.length<4 || name.length>6){
//window.alert("error");
error1.style.display="inline";
return false;
}
if(passwd!=passwd2){
error2.innerText="两次密码不一致";
error2.style.display="inline";
return false;
}
}
</script>
</head>
<body>
<h1>用户注册</h1>
<form action="">
<table border="0">
<tr>
<td>用户 名:</td><td class="style2"><input class="style1" type="text" id="username" name="username"/>
<span style="display: none;color: red;font-size: 10px" id="error1">用户名必须在4-6位间</span>
</td>
</tr>
<tr><td>密 码:</td><td><input class="style1" type="password" name="passwd" id="passwd"/></td></tr>
<tr>
<td>确认密码</td><td><input class="style1" type="password" name="passwd2" id="passwd2"/>
<span style="display: inline;color: red;" id="error2"></span>
</td>
</tr>
<tr>
<td>年 龄</td><td><input class="style1" type="text" name="age" id="age"/></td>
</tr>
<tr><td>电子邮件</td><td><input class="style1" type="text" name="email" id="email"/></td></tr>
<tr><td>电话号码</td><td><input class="style1" type="text" name="phone" id="phone"/></td></tr>
<tr><td><input type="submit" οnclick="return check();" value="注册用户"></td>
<td><input type="reset" value="重新填写"/></td></tr>
</table>
</form>
</body> </html>
案例:文本框中自动设置鼠标光标!
<html>
<head>
</head>
<body>
<form>
name:<input type="text" id="text1" /> age: <input type="text" id="text2" /> <br />
<input type="button" οnclick="setFocus()" value="Set focus" />
<input type="button" οnclick="loseFocus()" value="Lose focus" />
</form>
</body>
<script type="text/javascript">
document.getElementById("text1").focus();
</script> </html>