Html5 number input step and precision

2019-01-23 13:19发布

问题:

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

回答1:

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



回答2:

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:

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



回答3:

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.