form对象



<form name=“form1” action=“login.php” method=“post”></form> form对象的属性 name:表单名称 method:提交方式,有get和post action:处理程序 enctype:表单数据的加密方式 …… Form对象的方法: submit():表单提交方式 reset():重置表单 form对象的事件 onsubmit:当点击“提交按钮”时发生,onsubmit事件发生的时机,是在单击“提交按钮”之后和数据发往服务前 onreset:重置事件 表单提交的三种方法 (1)submit按钮结合onsubmit事件,实现表单提交(最常用) <script type="text/javascript"> function checkForm() { var theForm = document.form1; if(theForm.username.value=="") { window.alert("用户名不能为空"); return false; }else if(theForm.userpwd.value.length==0) { window.alert("密码不能为空"); return false; }else { window.alert("验证通过"); return true; } } </script> </head> <body> <form name="form1" method="get" action="login.php" onsubmit="return checkForm()"> 用户名:<input type="text" name="username" /> 密码:<input type="password" name="userpwd" /> <input type="submit" value="提交表单" /> </form> (2)submit按钮结合onclick事件,实现表单提交 函数定义跟上面一样…… <form name="form1" method="get" action="login.php"> 用户名:<input type="text" name="username" /> 密码:<input type="password" name="userpwd" /> <input type="submit" value="提交表单" onclick="return checkForm()" /> </form> (3)button按钮结合submit()方法,实现表单提交 <script type="text/javascript"> function checkForm() { var theForm = document.form1; if(theForm.username.value=="") { window.alert("用户名不能为空"); }else if(theForm.userpwd.value.length==0) { window.alert("密码不能为空"); }else { window.alert("验证通过"); theForm.submit(); //提交表单的方法 } } </script> </head> <body> <form name="form1" method="get" action="login.php"> 用户名:<input type="text" name="username" /> 密码:<input type="password" name="userpwd" /> <input type="button" value="提交表单" onclick="checkForm()" /> </form> 提交信息 onsubmit和onclick事件:它们的返回值,会影响默认动作的执行。 submit按钮的默认动作就是提交表单; <a>超级链接的默认动作,就是打开一个外部链接; 当事件返回false时,表单才会阻止提交,其它的返回值,表单都会提交。