[英]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;">   BIOLOGY </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.