简体   繁体   中英

HTML5 input type=number and the placeholder attribute

有没有办法在<input type=number>上使用占位符?

I've just checked against the HTML5 spec for input where a type is number and it explicitly says that the placeholder attribute may not be used . I also had errors with http://validator.w3.org when I tried to validate html with an input type of number with a placeholder attribute. Looks like browsers may support this but it's not valid in the HTML5 spec.

@zachleat is correct, the placeholder attribute is allowed in the latest HTML5 spec:

HTML 5.1 2nd Edition: forms, number-state-typenumber (see the bookkeeping section)

The following common input element content attributes apply to the element: autocomplete, list, max, min, placeholder , readonly, required, and step content attributes;

Here is the Placeholders section in the MDN Web Docs about <input type="number"> : https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number#Placeholders

Sometimes it's helpful to offer an in-context hint as to what form the input data should take. This can be especially important if the page design doesn't offer descriptive labels for each <input> . This is where placeholders come in. A placeholder is a value that demonstrates the for the value should take by presenting an example of a valid value, which is displayed inside the edit box when the element's value is "" . Once data is entered into the box, the placeholder disappears; if the box is emptied, the placeholder reappears.

Using Placeholder: <input type="number" placeholder="10">

Using Min and Max Values: <input type="number" placeholder="10">

Using Value: <input type="number" value="10">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM