HTML5中是否有浮点输入类型? [英] Is there a float input type in HTML5?

查看:117
本文介绍了HTML5中是否有浮点输入类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 html5.org ,数字输入类型的值属性,如果指定而不是空的,必须有一个有效的浮点数值。



然而,简单地说(在最新版本的Chrome中,无论如何),updown 用整数控制,而不是浮点数:



< input type =number id =totalAmt>< / input>



是否存在一个原生于HTML5的浮点输入元素,或者使数字输入类型使用浮动而不是ints的方式?或者我必须使用jQuery UI插件吗?

解决方案

数字类型具有控制哪些数字有效的 step 值(以及 max min ),默认为 1 。此值也用于步进按钮的实现(即按下增加 step )。



只需更改这个值适用于任何合适的值。对于金钱来说,预计可能会出现两位小数:

 < input type =numberstep =0.01> 

(我还会设置 min = 0 如果它只能是肯定的)



如果您希望允许任意数量的小数位,您可以使用 step =any (尽管对于货币,我建议坚持 0.01 )。在Chrome& Firefox,当使用任何时,步进按钮将递增/递减1。 (感谢Michal Stefanow的回答指出任何,以及在这里查看相关的规范

这里是一个操场,显示各个步骤如何影响各种输入类型:



 < form> < input type = number step = 1 />第1步(默认)< br /> <输入类型=编号步骤= 0.01 />步骤0.01< br /> < input type = number step = any />步骤任何< br /> <输入类型=范围步长= 20 />步骤20< br /> < input type = datetime-local step = 60 />步骤60(默认)< br /> < input type = datetime-local step = 1 />第1步< br /> < input type = datetime-local step = any />步骤任何< br /> < input type = datetime-local step = 0.001 />步骤0.001< br /> < input type = datetime-local step = 3600 />第3600步(1小时)< br /> < input type = datetime-local step = 86400 />步骤86400(1天)< br /> <输入类型=日期时间 - 本地步骤= 70 />步骤70(1分钟,10秒)< br />< / form>  






像往常一样,我会添加一个快速提示:请记住,客户端验证对用户来说只是一种方便。您还必须在服务器端验证!


According to html5.org, the "number" input type's "value attribute, if specified and not empty, must have a value that is a valid floating point number."

Yet it is simply (in the latest version of Chrome, anyway), an "updown" control with integers, not floats:

<input type="number" id="totalAmt"></input>

Is there a floating point input element native to HTML5, or a way to make the number input type work with floats, not ints? Or must I resort to a jQuery UI plugin?

解决方案

The number type has a step value controlling which numbers are valid (along with max and min), which defaults to 1. This value is also used by implementations for the stepper buttons (i.e. pressing up increases by 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)

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). In Chrome & Firefox, the stepper buttons will increment / decrement by 1 when using any. (thanks to Michal Stefanow's answer for pointing out any, and see the relevant spec here)

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!

这篇关于HTML5中是否有浮点输入类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆