CGIでHTMLを吐き出す時に任意のテキストボックスにカーソルを持っていくには
Perl等のCGIで、テキストボックスの入力値チェック(DBと比較とか)をした後、エラーのあるテキストボックスにカーソルを持っていきたい場合があります。
そのような時は、以下のように、CGIでbody要素にonLoad属性を設定すれば、いけますね。
<html> <head> <meta http-equiv="content-type" content="text/html; charset=shift_jis"> <meta http-equiv="content-script-type" content="text/javascript"> <title>テスト</title> </head> <body onLoad='document.F01.I03.focus()'> <form action="." name="F01"> <p><input type="text" name="I01"></p> <p><input type="text" name="I02"></p> <p><input type="text" name="I03"></p> </form> </body> </html>
尚、DOMを使うと以下の感じ
<html> <head> <meta http-equiv="content-type" content="text/html; charset=shift_jis"> <meta http-equiv="content-script-type" content="text/javascript"> <title>テスト</title> </head> <body onLoad='document.getElementById("ID02").focus()'> <form action="." name="F01"> <p><input type="text" name="I01" id="ID01"></p> <p><input type="text" name="I02" id="ID02"></p> <p><input type="text" name="I03" id="ID03"></p> </form> </body> </html>
尚、上記サンプルは、カーソルを2番目のテキストボックスに設定しています。