简体   繁体   English

我如何获取按钮值以显示在文本框中

[英]how do i get the button value to display in the text box

This is my code, how do i get the button value to display in the text box. 这是我的代码,如何获取按钮值以显示在文本框中。 I tried this but it didn't work 我尝试了这个,但是没有用

<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>

this is a link from fiddle where i tried it https://jsfiddle.net/mp5pndyw/ 这是我尝试过的小提琴的链接https://jsfiddle.net/mp5pndyw/

Here's your fiddle modified to work. 这是您的小提琴经过修改后可以工作的地方。

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

 var buttons = document.querySelectorAll('.dropdown-item') var textbox = document.getElementById('display') for (let i=0; i < buttons.length; i++) { buttons[i].addEventListener("click", function(e) { textbox.value = e.target.value }) } 
 <form method = "POST" action = "addprocess.php"> <fieldset> <legend align="center">Semester Courses</legend><br><br> <div class="row"><label style="font-family: verdana; font-size: 25px;">&emsp;&emsp;&emsp;BIOLOGY&emsp;</label> <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> 

You are setting the innerHTML of the text input. 您正在设置文本输入的innerHTML Instead, you need to use value 相反,您需要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> 

I'm not sure precisely why your example is not working, when attempting it I kept getting a console error that it violates a policy. 我不确定您的示例为何无法正常工作,尝试该示例时,我不断收到控制台错误,表明它违反了政策。 If however you just want it to work, you could always remove your onclick attribute and use the following javascript: 但是,如果您只是希望它能正常工作,则可以始终删除onclick属性,并使用以下javascript:

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

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

This adds the listener via javascript (ie addEventListener... ). 这通过javascript(即addEventListener ...)添加了侦听器。 You can then access the element you clicked via the event (e), also making the function reusable for similar situations. 然后,您可以通过事件(e)访问单击的元素,也可以使该函数在类似情况下可重复使用。 Happy coding! 编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取输入字段的值并将其显示在对话框中? - How do I get the value of input fields and display it on the dialog box? 如何获取单选按钮的文本(不是值) - How do I get the text of a radio button (not the value) 单击具有指定功能的按钮时,如何在输出框中的下拉列表中显示选项的值? - How do I display the value of an option on a drop down in an output box when a button with an assigned function is clicked on? 如何从iframe内部的选择框中获取所选值,并将其显示在外部? - How do I get the selected value from a select box, from inside an Iframe, and display it on the outside? 如何在输入文本框中显示选定的值 - how can i display selected value in input text box 如何通过单击将按钮的值添加到文本框? - How can I add the value from button to the text box by click? 如何获取通过JQuery插入到表行中的下拉框的值/文本 - How do I get the value/text of a dropdown box that got inserted into a table row via JQuery 如何从 selenium 的输入文本框中获取文本? - How do I get text from input text box in selenium? 如何显示输入框的更新值? - How do I display updated value of input box? 在文本框中显示按钮的值并更改 javascript 中选定按钮的颜色 - Display the value of button in the text box and change the color of the selected button in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM