<input type="number" id="totalAmt"></input>
HTML5中是否有原生的浮点数输入元素,或者有什么方法可以让数字输入类型使用浮点数而不是整数?还是我必须使用jQuery UI插件?
<input type="number" id="totalAmt"></input>
HTML5中是否有原生的浮点数输入元素,或者有什么方法可以让数字输入类型使用浮点数而不是整数?还是我必须使用jQuery UI插件?
number
类型有一个step
值,控制哪些数字是有效的(与max
和min
一起),默认为1
。实现还使用此值来进行增量按钮的操作(即按向上按钮增加step
)。
只需将此值更改为适当的值即可。对于货币,可能期望两位小数:
<label for="totalAmt">Total Amount</label>
<input type="number" step="0.01" id="totalAmt">
(如果它只能为正数,则我建议将min=0
)
如果您想允许任意数量的小数位数,可以使用 step="any"
(尽管对于货币来说,我建议坚持0.01
)。在Chrome和Firefox中,当使用any
时,步进器按钮将增加/减少1。感谢Michal Stefanow的答案指出了any
,请参见相关规范。
这里有一个游乐场,展示了各种步骤如何影响各种输入类型:
<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>
一如既往,我要补充一句话:记住,客户端验证只是为了方便用户。你还必须在服务器端进行验证!
来源:http://blog.isotoma.com/2012/03/html5-input-typenumber-and-decimalsfloats-in-chrome/
但如果您想要所有的数字都有效,包括整数和小数怎么办?在这种情况下,请将步长设置为“any”。
<input type="number" step="any" />
在 Chrome 上可行,其他浏览器未测试。
你可以使用:
<input type="number" step="any" min="0" max="100" value="22.33">
您可以使用步骤属性来设置数字输入框的步进值:
<input type="number" id="totalAmt" step="0.1"></input>
step="any"
将允许任何小数。
step="1"
将不允许小数。
step="0.5"
将允许0.5、1、1.5等;
step="0.1"
将允许0.1、0.2、0.3、0.4等;
基于这个答案
<input type="text" id="sno" placeholder="Only float with dot !"
onkeypress="return (event.charCode >= 48 && event.charCode <= 57) ||
event.charCode == 46 || event.charCode == 0 ">
含义:
字符编码:
0,1,2,3,4,5,6,7,8,9
Backspace
(否则需要在 Firefox 上刷新页面)点
&&
是 AND
,||
是 OR
运算符。
如果你尝试使用逗号进行浮点数:
<input type="text" id="sno" placeholder="Only float with comma !"
onkeypress="return (event.charCode >= 48 && event.charCode <= 57) ||
event.charCode == 44 || event.charCode == 0 ">
支持的浏览器有Chromium和Firefox (Linux X64)(其它浏览器我不存在。)
|| event.ctrlKey
以允许基本的复制粘贴。但如果这不是原型,我不会使用这种方法。 - yellowsir我这样做
<input id="relacionac" name="relacionac" type="number" min="0.4" max="0.7" placeholder="0,40-0,70" class="form-control input-md" step="0.01">
然后,我定义了最小值为0.4,最大值为0.7,步长为0.01:0.4、0.41、0.42......0.7。
<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>
我已经开始使用 inputmode="decimal"
,它可以与智能手机完美配合使用:
<input type="text" inputmode="decimal" value="1.5">
请注意,我们必须使用 type="text"
而不是 number
。然而,在桌面上,它仍然允许字母作为值。
对于桌面计算机,您可以使用以下内容:
<input type="number" inputmode="decimal">
它只允许输入 0-9
和 .
,并且只接受数字。
请注意,一些国家使用 ,
作为小数分隔符,这在 NumPad 上被默认激活。因此,如果您的网站有国际用户,则通过 NumPad 输入浮点数将无法正常工作,因为输入字段期望一个 .
(在 Chrome 中)。这就是为什么您应该使用 type="text"
。
您可以在桌面(包括使用NumPad)和手机上尝试这个功能:
<p>Input with type text:</p>
<input type="text" inputmode="decimal" value="1.5">
<br>
<p>Input with type number:</p>
<input type="number" inputmode="decimal" value="1.5">
是的,这是正确答案:
step="any"
这样更有效率。相信我。
<input type="number" step="any">
document.getElementById('form1').addEventListener('submit', function(e){
e.preventDefault();
alert("Your nnumber is: "+document.getElementById('n1').value)
alert("This works no ? :) please upvote")
})
<form id="form1">
<input type="number" step="any" id="n1">
<button type="submit">Submit</button>
</form>
<!-- UPVOTE :)-->