I think what's missing in the answers above is the need to specify a different value for the step
attribute, which has a default value of 1
.
(我认为上述答案中缺少的是需要为step
属性指定不同的值,该属性的默认值为1
。)
If you want the input's validation algorithm to allow floating-point values, specify a step accordingly.(如果希望输入的验证算法允许浮点值,请相应地指定一个步骤。)
For example, I wanted dollar amounts, so I specified a step like this:
(例如,我想要美元金额,因此我指定了以下步骤:)
<input type="number" name="price"
pattern="[0-9]+([.,][0-9]+)?" step="0.01"
title="This should be a number with up to 2 decimal places.">
There's nothing wrong with using jQuery to retrieve the value, but you will find it useful to use the DOM API directly to get the elements's validity.valid
property.
(使用jQuery检索值没有错,但是您会发现直接使用DOM API来获取元素的validity.valid
属性很有用。)
I had a similar issue with the decimal point, but the reason I realized there was an issue was because of the styling that Twitter Bootstrap adds to a number input with an invalid value.
(我的小数点也有类似的问题,但我意识到存在问题的原因是因为Twitter Bootstrap的样式添加到具有无效值的数字输入中。)
Here's a fiddle demonstrating that the adding of the step
attribute makes it work, and also testing whether the current value is valid:
(这是一个小提琴,它证明了step
属性的添加使其起作用,并且还测试了当前值是否有效:)
TL;DR: Set the a step
attribute to a floating-point value, because it defaults to 1
.
(TL; DR: 将step
属性设置为浮点值,因为它的默认值为1
。)
NOTE : The comma doesn't validate for me, but I suspect that if I set my OS language/region settings to somewhere that uses a comma as the decimal separator, it would work.
(注意 :逗号不能为我验证,但是我怀疑如果我将OS语言/区域设置设置为使用逗号作为小数点分隔符的某个地方,它将起作用。)
*note in note*: that was not the case for OS language/keyboard settings *German* in Ubuntu/Gnome 14.04.(*注意*:在Ubuntu / Gnome 14.04中,操作系统语言/键盘设置*德语*并非如此。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…