2017-08-17 4 views
0

これは私のコードです。ボタンの値をテキストボックスに表示するにはどうすればいいですか? 私はこれを試してみましたが、それはボタンの値をテキストボックスに表示するにはどうすればいいですか

<p>Click the button to return the value of its value attribute.</p> 

<button id="myBtn" value="myvalue" onclick="myFunction()">Try it</button> 

<input id="demo" readonly> 


<script> 
    function myFunction() { 
    var x = document.getElementById("myBtn").value; 
    document.getElementById("demo").innerHTML = x; 
    } 
</script> 

を動作しませんでしたこれは私がそれをhttps://jsfiddle.net/mp5pndyw/

+0

テキストボックスに値を表示するには、 'innerHTML'ではなく' value'を使用してください。 – urielSilva

+1

'document.getElementById(" demo ")。value = x'の場合、input要素はHTMLではない値を表示します。また、そのフィドルのコードは、質問とはまったく異なります。 –

+1

あなたの入力に 'type'を割り当てておらず、入力に' value'sがあり、 'innerHTML'も含まれていません。あなたのjsfiddleもあなたの質問のソースコードとは非常に異なっています。 – NewToJS

答えて

0

ここで動作するように変更され、あなたのフィドルだを試してみましたフィドルからのリンクがあります。

https://jsfiddle.net/mp5pndyw/1/

var buttons = document.querySelectorAll('.dropdown-item') 
 
var textbox = document.getElementById('display') 
 

 
for (let i=0; i < buttons.length; i++) { 
 
\t buttons[i].addEventListener("click", function(e) { 
 
\t \t textbox.value = e.target.value 
 
\t }) 
 
}
<form method = "POST" action = "addprocess.php"> 
 
\t <fieldset> 
 
\t <legend align="center">Semester Courses</legend><br><br> 
 
\t <div class="row"><label style="font-family: verdana; font-size: 25px;">&emsp;&emsp;&emsp;BIOLOGY&emsp;</label> 
 
    \t <div class="col-lg-4"> 
 
    <div class="input-group"> 
 
     <input type="text" id="display" class="form-control" aria-label="Text input with dropdown button" readonly> 
 
     <div class="input-group-btn"> 
 
     <button type="button" id="dropdown0" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> 
 
      Click to Select 
 
     </button> 
 
    
 
     <div class="dropdown-menu dropdown-menu-right"> 
 
      <button type="button" class="dropdown-item" value="BIOL 8803">BIOL 8803</button> 
 
      <div role="separator" class="dropdown-divider"></div> 
 
      <button type="button" class="dropdown-item" value="BIOL 8805">BIOL 8805</button> 
 
      <div role="separator" class="dropdown-divider"></div> 
 
      <button type="button" class="dropdown-item" value="BIOL 8807">BIOL 8807</button> 
 
      <div role="separator" class="dropdown-divider"></div> 
 
      <button type="button" class="dropdown-item" option value="BIOL 8809">BIOL 8809</button> 
 
     
 
     </div> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div><br><br> 
 
<input name="submit" type ="submit" value="click to submit"> 
 
<input name="reset" type ="reset" value="Reset"> 
 
</fieldset> 
 
</form>

+0

あなたが行った変更を指摘し、その理由を説明して、現在のソースコードがなぜ意図したとおりに機能していますか? – NewToJS

+0

ありがとう@Christopher Messerがこれで動作しました – ebi

+0

@Christopher Messerあなたのブラウザでコードを実行してみてください。私の凡例はフィドルにのみ表示されますが、ブラウザには表示されません。それがなぜそうであるかについてのあなたのアイデアはありますか? – ebi

0

は、テキスト入力のinnerHTMLを設定しています。代わりに、私はそれがポリシーに違反しているコンソールエラーを取得保管、それをしようとしたとき、あなたの例では、動作していない、正確に、なぜわからないんだけどvalue

function myFunction() { 
 
    var x = document.getElementById("myBtn").value; 
 
    document.getElementById("demo").value = x; 
 
}
<p>Click the button to return the value of its value attribute.</p> 
 

 
<button id="myBtn" value="myvalue" onclick="myFunction()">Try it</button> 
 

 
<input id="demo" readonly>

0

使用する必要があります。あなたはちょうどそれが仕事をしたいしかし、あなたは常に次のjavascriptあなたのonclick属性を削除し、使用することができた場合:

function myFunction(e) { 
    var myInput = document.getElementById("demo") 
    myInput.value = e.srcElement.value 
} 

document.getElementById("myBtn").addEventListener("click", myFunction) 

これはJavaScriptでリスナーを追加します(つまり、addEventListenerを...)。イベント(e)でクリックした要素にアクセスし、同様の状況で再利用できるようにすることができます。ハッピーコーディング!

関連する問題