Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

html5 - HTML5中是否有浮点输入类型?(Is there a float input type in HTML5?)

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."

(根据html5.org的说法 ,“数字”输入类型的“值属性,如果指定且不为空,则必须具有一个有效浮点数的值”。)

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

(但这只是简单的(无论如何,在最新版的Chrome中),是带有整数而不是浮点数的“上下”控件:)

 <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?

(是否有HTML5固有的浮点输入元素,或使数字输入类型适用于浮点而不是整数的方法?)

Or must I resort to a jQuery UI plugin?

(还是我必须诉诸jQuery UI插件?)

  ask by B. Clay Shannon translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The number type has a step value controlling which numbers are valid (along with max and min ), which defaults to 1 .

(number类型具有一个step值,用于控制哪些数字有效(以及maxmin ),默认值为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!

(您还必须在服务器端进行验证!)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...