简体   繁体   English

如何在单击按钮时在输入字段中显示文本?

[英]How to display a Text in an input field on a button click?

i want any text like hello world to be appeared in a input field when i click the button.当我点击按钮时,我希望任何像 hello world 这样的文本出现在输入字段中。 How can i do that?我怎样才能做到这一点?

enter code here

<button class='submit' onclick="myFunction()"> Click Me </button>
<input type= 'text' id='demo'>

Use eventListeners via addEventListener通过addEventListener使用 eventListeners

 window.addEventListener("load", onLoadHandler); function onLoadHandler(){ let button = document.getElementById("myButton"); button.addEventListener("click",buttonClick); } function buttonClick(e){ let input = document.getElementById("textInput"); input.value = "Hello world;"; }
 <div> <input id="textInput"> </div> <div> <button id="myButton"> write in input </button> </div>

暂无
暂无

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

相关问题 单击“ X”按钮时删除placehoder输入文件名,并显示默认输入字段文本 - Remove placehoder input file name when click on “X” button, and display default input field text 单击按钮即可在文本字段上获取用户输入 - Get the user input on text field on button click 在按钮单击时将文本附加到输入字段 - Appending text to input field on button click 如何一次只显示一个文本框,然后单击以显示下一个显示下一个输入字段 - How to display only one text box at a time and click to next display next input field 在文本字段中输入文本,单击按钮并在页面上获取结果(JQuery) - Input a text into a text field, click a button and get the result on the page (JQuery) 如果单击按钮,如何删除输入字段? - How to delete input field if I click on button? 单击时如何将文本追加到输入字段? - how to append text to an input field on click? 无法弄清楚如何获取按钮单击事件的值并将其粘贴到文本输入字段中 - Can't figure out how to grab value of button click event and paste it into a text input field 当用户单击按钮时,如何使其自动在文本字段中输入特定的关键字? - How can I make it automatically input particular keyword into text field when a user click a button? 如何自动增加按钮单击时动态添加的输入字段(类型=文本)中的值? - how to Auto Increment the value in input field (type=text) that is dynamically added on a button click?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM