Javascript Html5 数字输入步长和精度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14365348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Html5 number input step and precision
提问by Yacine Zalouani
I can't find out how to define a step value and a precision to a input[number]
我不知道如何为 input[number] 定义步长值和精度
1.01is considered as invalid until I specify a step of 0.01.
But In that case I can't specify a specific step.
1.01被认为是无效的,直到我指定了一个步骤0.01。但在那种情况下,我无法指定具体步骤。
Same issue with big number. If I specify a step of 1000and the user type 1001the value is considered invalid...
大数同样的问题。如果我指定了一个步骤1000并且用户键入1001该值被认为是无效的...
You can have a look to this example
你可以看看这个例子
回答by int32_t
I guess you want to disable step validation. If so, step="any"should work.
我猜您想禁用步骤验证。如果是这样,step="any"应该工作。
回答by Bernardino Frola
You probably just need to define a correct step and an initial valuethat suits your purpose.
您可能只需要定义适合您目的的正确步骤和初始值。
The role of the valueattribute is not fully explained in the stepattribute documentation http://www.w3schools.com/tags/att_input_step.asp.
value属性的作用在step属性文档http://www.w3schools.com/tags/att_input_step.asp 中没有完全说明。
In the first input in your example, attributes are set to value = 1.01and step = 1(default value). The input will accept the following values: 1.01 + 1n. where nis an integer value. Example of accepted values are: 1.01, 2.01, 3.01 and so on, as well as -0.99, -1.99 -1.99 and so on.
在您的示例的第一个输入中,属性设置为value = 1.01和step = 1(默认值)。输入将接受以下值:1.01 + 1n. 其中n是一个整数值。可接受值的示例有:1.01、2.01、3.01 等,以及 -0.99、-1.99 -1.99 等。
As a general rule, the accepted values will be:
作为一般规则,接受的值将是:


Where
.
哪里
。
You can have an idea of the accepted values by using the UP/DOWN arrow keys when the input is focused.
当输入焦点时,您可以通过使用向上/向下箭头键来了解可接受的值。
As suggested in previous answers, step="any"will disable the step validation, it won't disable the stepper functionality (stepwill defaults to 1), but will require to implement the step validation by hand.
正如之前的答案所建议的那样,step="any"将禁用步骤验证,它不会禁用步进器功能(step默认为 1),但需要手动实现步骤验证。
回答by Italo Borssatto
Take a look at the Definition and Usageof the stepattribute:
看看的定义和用法的的step属性:
The step attribute specifies the legal number intervals for an element.
step 属性指定元素的合法数字间隔。
Once it is out of the intervals, the number is illegal.
一旦超出间隔,该号码就是非法的。
Try using Javascript code to take care of your need.
尝试使用 Javascript 代码来满足您的需求。

