html - 单击时将按钮值添加到输入字段
问题描述
单击按钮时,我需要将按钮值添加到表单输入字段。
菜鸟的错误,我有一个工作版本,但我不小心替换了 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.
解决方案
下一次虽然你可能想先表现出一些努力。此外,您需要重新添加您的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>
推荐阅读
- iview-ui - 使用单选选择表中的特定行
- javascript - CSS 和 JS 文件有时无法在我们的 Wordpress 网站上加载
- unity3d - 如何在统一 3d 中单击“选项卡”按钮时聚焦输入字段
- angular - 如何使用 web api 和 angular 在表单提交上上传文件
- django - Django:我可以链接 2 个模型字段而不是模型类吗?
- react-native - React-native-tag-autocomplete:如何在建议列表中添加滚动条
- java - Java String spit 使用正则表达式和单个正斜杠,后面没有斜杠
- spring - Spring JPA:如何插入数据以使用@ManytoMany 关系连接多个表
- .net - .net 项目的 DLL 注入
- reactjs - 使用反应组件时间选择器时 value.format 不是一个功能