フォーム要素への空の入力の後にカーソルを追加したい。最初の空のフォームがある場所。
この目標で、私は機能に追加しようとしthis.focus();
ましvalidate()
た。しかし、これは成功しませんでした。
そして2番目のポイント - ブラウザでページが最初のフォーム要素に出現した後にカーソルを設定する方法。このターゲットonLoad();
メソッドをに試してみましたbody
。しかし、これは成功しませんでした。
コード:
<html>
<head>
<title>Form with check</title>
<script>
function validate() {
if(document.form1.yourname.value.length < 1) {
alert("Enter your name, please");
this.focus();
return false;
}
if(document.form1.adress.value.length < 3) {
alert("Enter your adress, please");
this.focus();
return false;
}
if(document.form1.phone.value.length < 3) {
alert("Enter your phone number, please");
this.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<h1>Form with check</h1>
<p>Input all data. When button Submit pushed data will be sent as message.</p>
<form name="form1" action="mailto:user@host.com" enctype="text/plain"
onSubmit="validate();">
<p><b>Name:</b><input type="text" length="20" name="yourname">
</p>
<p><b>Adress:</b><input type="text" length="20" name="adress">
</p>
<p><b>Phone:</b><input type="text" length="20" name="phone">
</p>
<input type="SUBMIT" value="Submit">
</form>
onLoad();
</body>
</html>
質問:
- この機能をフォームに追加するにはどうすればよいですか?