首页 > 解决方案 > 单击时将按钮值添加到输入字段

问题描述

单击按钮时,我需要将按钮值添加到表单输入字段。

菜鸟的错误,我有一个工作版本,但我不小心替换了 html 文件。我不记得我是怎么做到的。我知道它不是 JS 它是 html 文件上的 OnClick 函数

<input id="userInput1" value="0">

<button id="p1"  value= "0" class="userbutton w-button" onclick="userinput1.display.value, RunGame()">0</button>


I expect it to add the button value to the input field.

标签: html

解决方案


下一次虽然你可能想先表现出一些努力。此外,您需要重新添加您的runGame()声明。为了工作示例而删除,

例如;onclick="setInputValue(this.innerHTML);runGame()"

干杯

setInputValue = (val) => {

  document.getElementById('userInput1').value = val;

}
<input id="userInput1" value="0">

<br/><br/>
<button id="p1" value="5" 
        class="userbutton w-button"
        onclick="setInputValue(this.innerHTML)">From HTML content</button>
        
<br/><br/>
or to get it from the value attribute;
<br/><br/>

<button id="p1" value="From the Value Attribute" 
        class="userbutton w-button"
        onclick="setInputValue(this.value)">From Attribute</button>


推荐阅读