How to get the raw value an <input type=“number”> field?

前端 未结 4 1565
眼角桃花
眼角桃花 2020-11-22 11:57

How can i get the \"real\" value of an field?


I have an input box, and i\'m using newer HTML5

4条回答
  •  北海茫月
    2020-11-22 12:35

    According to the WHATWG, you shouldn't be able to get the value unless it's valid numeric input. The input number field's sanitization algorithm says the browser is supposed to set the value to an empty string if the input isn't a valid floating point number.

    The value sanitization algorithm is as follows: If the value of the element is not a valid floating-point number, then set it to the empty string instead.

    By specifying the type () you're asking the browser to do some work for you. If, on the other hand, you'd like to be able to capture the non-numeric input and do something with it, you'd have to rely on the old tried and true text input field and parse the content yourself.

    The W3 also has the same specs and adds:

    User agents must not allow the user to set the value to a non-empty string that is not a valid floating-point number.

提交回复
热议问题