首页 > 解决方案 > 计算每次按钮单击并在输入类型号上显示

问题描述

我有一个功能,每次单击它时,我的按钮都会添加一个表格行。我的目标是,如何在输入类型 =“数字”中显示按钮单击的值?我的输入类型的值应该从“1”开始,每次单击按钮时,递增1。提前谢谢你。

HTML:

 <button type="button" id="clickCount" onClick="clickMe" value="click me"></button>
 <input type="number" id="showMe" value="1"> // the value of my button clicks should be displayed here

标签: javascript

解决方案


用这个:

document.getElementById("clickCount").addEventListener("click", function() {
  document.getElementById("showMe").value++;
})
<input type="number" id="showMe" value="1">
<button type="button" id="clickCount">Click Me</button>


推荐阅读