Autocomplete text input

前端 未结 5 802
夕颜
夕颜 2020-12-29 03:14

I know there are a lot of JavaScript solutions, but is there an HTML5 method of having a text input with autocomplete? The datalist element is almost what I\'m after, except

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-29 03:40

    HTML5 has an autocomplete attribute which can be specified as either on or off in a field.

    Here's an example:

    First name:
    Last name:
    E-mail:

    The way it works is that the values that you submit the first time will be suggested to you on subsequent times you visit this page on keyup of each field.

    • The key issue in deciding to use this feature is one of browser compatibility. Your best bet is checking multiple browsers to make sure it works. caniuse.com makes the support looks pretty bad, but I don't see any harm it using it to help those who have modern browers.

    Other factoids about autocomplete from W3Schools, don't hate in this case as it does cover the basics:

    • When autocomplete is on, the browser automatically complete values based on values that the user has entered before.
    • It is possible to have autocomplete "on" for the form, and "off" for specific input fields, or vice versa.
    • The autocomplete attribute works with and the following types: text, search, url, tel, email, password, datepickers, range, and color.

提交回复
热议问题