您可以使用JavaScript编写以下代码来实现按钮点击后输入框内数字加1和减1的功能:

HTML代码:

<input type="number" id="myInput" value="0">
<button onclick="increment()">+1</button>
<button onclick="decrement()">-1</button>

JavaScript代码:

function increment() {
  var input = document.getElementById("myInput");
  input.value = parseInt(input.value) + 1;
}

function decrement() {
  var input = document.getElementById("myInput");
  input.value = parseInt(input.value) - 1;
}

上述代码中,我们首先为输入框和两个按钮定义了相应的id,然后在JavaScript代码中通过document.getElementById方法获取到相应的元素。在增加按钮(+1)的点击事件处理函数increment()中,我们首先获取输入框的值,并通过parseInt函数将其转换为整数。然后将获取到的值加1,并将结果赋值给输入框。类似地,在减少按钮(-1)的点击事件处理函数decrement()中,我们将获取到的值减1,并将结果赋值给输入框。这样每次点击按钮时,输入框内的数字就会相应地增加或减少1

用js为button实现点击后 input内数字+1和-1功能

原文地址: http://www.cveoy.top/t/topic/hAGZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录