電子書籍の厳選無料作品が豊富!

HTMLの範囲ではないかも知れませんが、どう行えば良いか分かりませんので、質問させて頂きますm(_ _)m

【        A          】 ←テキストボックス

【テスト】 【テスト1】 【テスト2】  ←ボタン

上記の構図で、ボタンのテスト1を押すと、テキストボックス内に【テスト】と勝手に打ち込まれる。
この様にしたいのですが、どうすればいいでしょうか?

分かりづらいかも知れませんが、答えて下されば幸いです。

A 回答 (5件)

javascriptですね。


簡単なサンプルを書くと、こう。

<p><input type="text" id="A"></p>
<p>
<input type="button" value="テスト1"
onclick="document.getElementById('A').value='テスト'">
<!-- idを付けておいて、そのidで対象を特定、
inputなので value の値を設定する -->
<input type="button" value="テスト2">
<input type="button" value="テスト3">
</p>
    • good
    • 0
この回答へのお礼

見事出来ました。
有り難うございました。

お礼日時:2007/07/15 16:53

解決しているみたいだが、何となくアレなので


#3の修正。どうやら入力された値はgetAttributeとかfirstChild.nodeValueでは取得できないようで.valueである必要があるようだ。
回答前のテストしてから投稿直前に変えちゃったもんで・・・(汗
========
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dt …
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<title>Q3167024 テストケース1</title>
<script type="text/javascript">

var textarea;

function init(){
textarea = document.getElementById("sample");
}

function InputTextByString(str){

textarea.value = str;

}

function AppendTextByString(str){


textarea.value += str;

}

function InputTextByButton(Button){

textarea.value = Button.value;

}

function AppendTextByButton(Button){


textarea.value += Button.value;

}


</script>
</head>
<body onload="init();">
<h1>テストケース</h1>

<!-- textareaに空白がないとfirstChildが取得できなかったもんで-->
<p><textarea id="sample" cols="100" rows="10"> </textarea></p>
<h2>パターン1</h2>

<p><input type="button" value="テスト" onclick="InputTextByString('あああ');" /><input type="button" value="テスト1" onclick="InputTextByString('テスト');" /><input type="button" value="テスト2" onclick="InputTextByString('いいい');" /></p>

<h2>パターン2</h2>

<p><input type="button" value="テスト" onclick="AppendTextByString('あああ');" /><input type="button" value="テスト1" onclick="AppendTextByString('テスト');" /><input type="button" value="テスト2" onclick="AppendTextByString('いいい');" /></p>

<h2>パターン3</h2>

<p><input type="button" value="テスト" onclick="InputTextByButton(this);" /><input type="button" value="テスト1" onclick="InputTextByButton(this);" /><input type="button" value="テスト2" onclick="InputTextByButton(this);" /></p>

<h2>パターン3</h2>

<p><input type="button" value="テスト" onclick="AppendTextByButton(this);" /><input type="button" value="テスト1" onclick="AppendTextByButton(this);" /><input type="button" value="テスト2" onclick="AppendTextByButton(this);" /></p>

<p><input type="text" id="TEXTBOX" /></p>
<p><input type="button" id="APPENDTEXTAREA" value="テキストボックスの内容を追記" onclick="AppendTextByString(document.getElementById('TEXTBOX').value);"/></p>

</body>
</html>
=========
    • good
    • 0

・テキストボックスに表示される


・テキストボックスに打ち込まれる(勝手に打ち込まれる)
の違いを詳しく教えてもらえますか?


代替案っぽいものの提案ですが、
ボタンを押したときではなく、HTMLファイルを表示したときにすでに表示されてる状態にするなら、
わざわざJavaScriptを使わなくても、
<input type="text" name="A" value="テスト">
でいいと思います。
    • good
    • 0

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "

http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dt …
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<title>Q3167024 テストケース1</title>
<script type="text/javascript">

var textarea;

function init(){
textarea = document.getElementById("sample");
}

function InputTextByString(str){

textarea.firstChild.nodeValue = str;

}

function AppendTextByString(str){


textarea.firstChild.nodeValue += str;

}

function InputTextByButton(Button){

textarea.firstChild.nodeValue = Button.getAttribute("value");

}

function AppendTextByButton(Button){


textarea.firstChild.nodeValue += Button.getAttribute("value");

}


</script>
</head>
<body onload="init();">
<h1>テストケース</h1>

<!-- textareaに空白がないとfirstChildが取得できなかったもんで-->
<p><textarea id="sample" cols="100" rows="10"> </textarea></p>
<h2>パターン1</h2>

<p><input type="button" value="テスト" onclick="InputTextByString('あああ');" /><input type="button" value="テスト1" onclick="InputTextByString('テスト');" /><input type="button" value="テスト2" onclick="InputTextByString('いいい');" /></p>

<h2>パターン2</h2>

<p><input type="button" value="テスト" onclick="AppendTextByString('あああ');" /><input type="button" value="テスト1" onclick="AppendTextByString('テスト');" /><input type="button" value="テスト2" onclick="AppendTextByString('いいい');" /></p>

<h2>パターン3</h2>

<p><input type="button" value="テスト" onclick="InputTextByButton(this);" /><input type="button" value="テスト1" onclick="InputTextByButton(this);" /><input type="button" value="テスト2" onclick="InputTextByButton(this);" /></p>

<h2>パターン3</h2>

<p><input type="button" value="テスト" onclick="AppendTextByButton(this);" /><input type="button" value="テスト1" onclick="AppendTextByButton(this);" /><input type="button" value="テスト2" onclick="AppendTextByButton(this);" /></p>
<p>質問者さんの勘違いも念のために想定して色々作ってみる</p>
</body>
</html>
    • good
    • 0

参考URLをご覧ください。


ボタンを押すのではなく、カーソルで触れると表示されるタイプで、
JavaScriptを使ったものですが、どうでしょうか?

参考URL:http://www.geocities.co.jp/HeartLand-Kaede/3853/ …
    • good
    • 0
この回答へのお礼

早速の回答有り難う御座います。
表示では無く、勝手に打ち込まれる仕様のものを探しておりまして・・・。

お礼日時:2007/07/14 17:26

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!