Html5 number input step and precision

2019-01-23 13:24发布

I can't find out how to define a step value and a precision to a input[number]

1.01 is considered as invalid until I specify a step of 0.01. But In that case I can't specify a specific step.

Same issue with big number. If I specify a step of 1000 and the user type 1001 the value is considered invalid...

You can have a look to this example

3条回答
Bombasti
2楼-- · 2019-01-23 13:36

Take a look at the Definition and Usage of the step attribute:

The step attribute specifies the legal number intervals for an element.

Once it is out of the intervals, the number is illegal.

Try using Javascript code to take care of your need.

查看更多
姐就是有狂的资本
3楼-- · 2019-01-23 13:40

You probably just need to define a correct step and an initial value that suits your purpose.

The role of the value attribute is not fully explained in the step attribute documentation http://www.w3schools.com/tags/att_input_step.asp.

In the first input in your example, attributes are set to value = 1.01 and step = 1 (default value). The input will accept the following values: 1.01 + 1n. where n is 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.

As a general rule, the accepted values will be:

value + step * n

Where n \in  \mathbb{Z}.

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 (step will defaults to 1), but will require to implement the step validation by hand.

查看更多
戒情不戒烟
4楼-- · 2019-01-23 13:43

I guess you want to disable step validation. If so, step="any" should work.

查看更多
登录 后发表回答