The number
type has a step
value controlling which numbers are valid (along with max
and min
), which defaults to 1
.
(number
类型具有一个step
值,用于控制哪些数字有效(以及max
和min
),默认值为1
。)
This value is also used by implementations for the stepper buttons (ie pressing up increases by step
). (此值也被实现用于步进按钮(即,按step
增加)。)
Simply change this value to whatever is appropriate.
(只需将此值更改为适当的值即可。)
For money, two decimal places are probably expected: (为了赚钱,可能期望两位小数:)
<input type="number" step="0.01">
(I'd also set min=0
if it can only be positive)
((如果它只能是正数,我还将设置min=0
))
If you'd prefer to allow any number of decimal places, you can use step="any"
(though for currencies, I'd recommend sticking to 0.01
).
(如果您希望允许任意数量的小数位数,则可以使用step="any"
(尽管对于货币而言,我建议坚持0.01
)。)
In Chrome & Firefox, the stepper buttons will increment / decrement by 1 when using any
. (在铬和Firefox,步进按钮将增量/减量被1时使用any
。)
(thanks to Michal Stefanow's answer for pointing out any
, and see the relevant spec here ) ((感谢Michal Stefanow指出了any
的答案,并在此处查看相关规范 ))
Here's a playground showing how various steps affect various input types:
(这是一个游乐场,显示了各个步骤如何影响各种输入类型:)
<form> <input type=number step=1 /> Step 1 (default)<br /> <input type=number step=0.01 /> Step 0.01<br /> <input type=number step=any /> Step any<br /> <input type=range step=20 /> Step 20<br /> <input type=datetime-local step=60 /> Step 60 (default)<br /> <input type=datetime-local step=1 /> Step 1<br /> <input type=datetime-local step=any /> Step any<br /> <input type=datetime-local step=0.001 /> Step 0.001<br /> <input type=datetime-local step=3600 /> Step 3600 (1 hour)<br /> <input type=datetime-local step=86400 /> Step 86400 (1 day)<br /> <input type=datetime-local step=70 /> Step 70 (1 min, 10 sec)<br /> </form>
As usual, I'll add a quick note: remember that client-side validation is just a convenience to the user.
(像往常一样,我将简要说明一下:请记住,客户端验证对用户来说只是一种便利。)
You must also validate on the server-side! (您还必须在服务器端进行验证!)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…