2011-06-02 10 views
0

javascriptを使用してテキストフィールドにテキスト文字列を追加したいのですが、onClickまたはonLoadイベントを使用しないでください。JavaScriptを使ってテキストフィールドに文字列をプログラムで挿入する方法

私は、単にブラウザがそれを読んで、javascriptがhtmlの本文内にあるとすぐに、そのjavascriptを実行したいと思っています。

テキストフィールドが、私はこのような何かを試してみましたが、それは動作しません

<form action="" method="post" name="formname" id="user-registration-form"> 
    <input type="text" name="edit-form" id="edit-form" /> 
</form> 

のようなものです:

<script type="text/javascript"> edit-form.value="the new value"; </script> 

私はこれをどのように行うことができますか?

答えて

6

あなたは編集メールを想定しdocument.getElementById()

<script type="text/javascript"> document.getElementById("edit-mail").value="the new value"; </script> 

ことで、これを取得しようとしたのは

document.getElementById("edit-mail").value="he new value" 
0
 
<script type="text/javascript"> 
document.formname.edit-mail.value="the new value"; 
</script> 
+0

私はタイプミスと推定される答えを微調整しました。あなたが気にしないことを願っています。 – Rob

+0

@RobertRyanおかげでね...あなたはいい仕事をしてくれました:-) –

0

あなたのテキストボックスのIDですIDを持つすべてのフォーム要素の贅沢がありません。直接

document.getElementById('user-registration-form').elements['edit-form'].value = 'new value'; 
0

あなたがドン多くの時間を試してみてください

0
var textfield = document.getElementByName("x"); 
textfield.value = "My text"; 

またはD ocument.getElementByName("x").value = "My text";

の代わりに、あなたの要素を取得したい場合は、getElementByIdをを使用することができますgetElementByNameを使用した:これは、あなたがそれをフィールドには名前だけの属性を持つ動作させることができる方法でありますその名前の代わりにそのIDに依存します。

関連する問題