如何增加一个数字输入?

huangapple go评论69阅读模式
英文:

How can I increment a number input?

问题

当使用HTML input元素时,我尝试实现step属性。这允许我通过点击输入字段中的上/下箭头来将当前值增加100。

然而,step确定了合法值,因此它不适用于简单的增量。例如,如果我输入123,它将增加到200,而不是223。

是否有一种简单的解决方法来实现input元素的增量/减量功能?

英文:

When using HTML input elements I tried implementing the step attribute. This allowed me to add 100 to the current value by clicking the up/down arrows in the input field.

However, step determines legal values, so it won't work for simple increments. For example, if I type in 123, it will increase to 200, not 223.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// populate field
document.querySelector(&quot;input&quot;).value = &quot;123&quot;;

<!-- language: lang-html -->

&lt;input type=&quot;number&quot; step=&quot;100&quot; value=&quot;0&quot;/&gt;

<!-- end snippet -->

Is there an easy workaround for an increment/decrement function for the input element?

答案1

得分: 4

step pushes away from the attribute:

<input type="number" step="100">

UPDATE:
Also, as @Barmar and @SebastianSimon pointed out, you can use defaultValue:

input.defaultValue = 123;
英文:

step pushes away from the attribute:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const input = document.querySelector(&#39;input&#39;);
input.value = 123;
input.setAttribute(&#39;value&#39;, 123);

<!-- language: lang-html -->

&lt;input type=&quot;number&quot; step=&quot;100&quot;&gt;

<!-- end snippet -->

UPDATE:
Also, as @Barmar and @SebastianSimon pointed out, you can use defaultValue:

input.defaultValue = 123;

答案2

得分: 1

const input = document.getElementById("myInput");
const incrementBtn = document.getElementById("incrementBtn");
const decrementBtn = document.getElementById("decrementBtn");

incrementBtn.addEventListener("click", () => {
  input.stepUp(100); // 增加输入值100
});

decrementBtn.addEventListener("click", () => {
  input.stepDown(100); // 减少输入值100
});
<input id="myInput" type="number" value="123" min="0" step="1" />
<button id="incrementBtn">增加</button>
<button id="decrementBtn">减少</button>

在回调函数中,我们使用内置方法 stepUp()stepDown() 来分别增加和减少输入值100。这些方法确保正确修改输入值,无论step属性如何设置。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const input = document.getElementById(&quot;myInput&quot;);
const incrementBtn = document.getElementById(&quot;incrementBtn&quot;);
const decrementBtn = document.getElementById(&quot;decrementBtn&quot;);

incrementBtn.addEventListener(&quot;click&quot;, () =&gt; {
  input.stepUp(100); // Increment the input value by 1
});

decrementBtn.addEventListener(&quot;click&quot;, () =&gt; {
  input.stepDown(100); // Decrement the input value by 1
});

<!-- language: lang-html -->

&lt;input id=&quot;myInput&quot; type=&quot;number&quot; value=&quot;123&quot; min=&quot;0&quot; step=&quot;1&quot; /&gt;
&lt;button id=&quot;incrementBtn&quot;&gt;Increment&lt;/button&gt;
&lt;button id=&quot;decrementBtn&quot;&gt;Decrement&lt;/button&gt;

<!-- end snippet -->

Inside the callback functions, we use the built-in methods stepUp() and stepDown() to increment and decrement the input value by 100, respectively. These methods ensure the input value is modified correctly, regardless of the step attribute.

huangapple
  • 本文由 发表于 2023年6月15日 04:37:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定