提出方法と確認(rèn)方法のまとめ
送信と検証方法の概要
1. 送信ボタンをonsubmitイベントと組み合わせて使用??します(最も一般的に使用されます)
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>php.cn</title> <script type="text/javascript"> function checkForm() { //判斷用戶名是否為空 if(document.form1.username.value=="") { window.alert("用戶名不能為空!"); return false; }else { window.alert("驗(yàn)證通過!"); return true; } } </script> </head> <body> <form name="form1" method="post" action="login.php" onsubmit="return checkForm()"> 用戶名:<input type="text" name="username" /> 密碼:<input type="password" name="userpwd" /> <input type="submit" value="提交表單" /> </form> </body> </html>
2 . Submit ボタン、onclick イベントと組み合わせて、フォームの検証と送信を?qū)g裝します
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>php.cn</title> <script type="text/javascript"> function checkForm() { //判斷用戶名是否為空 if(document.form1.username.value=="") { window.alert("用戶名不能為空!"); }else { window.alert("驗(yàn)證通過!"); } } </script> </head> <body> <form name="form1" method="post" action="login.php"> 用戶名:<input type="text" name="username" /> 密碼:<input type="password" name="userpwd" /> <input type="submit" value="提交表單" onclick="checkForm()" /> </form> </body> </html>
3. submit() メソッドと組み合わせて、フォームの検証と送信を?qū)g裝します。提出