简体   繁体   English

自动完成 HTML 表单

[英]Auto complete for HTML forms

I am looking for answers regarding auto complete for form fields.我正在寻找有关表单字段自动完成的答案。

How can I auto fill common inputs like First name, last name, address..?如何自动填充常用输入,如名字、姓氏、地址..?

In Chrome, If the user has a Google profile, fields can be autofilled.在 Chrome 中,如果用户有 Google 个人资料,则可以自动填充字段。

It appears IE offers similar functionality:看来 IE 提供了类似的功能:

(From MS Website) "You can use AutoComplete to store passwords and other information that you type into web form fields. When you turn on AutoComplete, Internet Explorer will automatically fill in the fields you type into web forms frequently, like your name and address." (来自 MS 网站)“您可以使用自动完成功能来存储您在网络表单字段中输入的密码和其他信息。当您打开自动完成功能时,Internet Explorer 将自动填写您经常在网络表单中输入的字段,例如您的姓名和地址.”

Is there any way I can ensure autofill happens, cross-browser, using HTML/JS?有什么方法可以确保自动填充发生,跨浏览器,使用 HTML/JS?

在此处输入图片说明

This question is pretty old but I have an updated answer for 2017 !这个问题很老了,但我有2017 年更新答案

Now there IS a cross-browser standard on how to name your <input> tags in order to trigger autocomplete.现在关于如何命名一个跨浏览器标准<input>以触发自动完成标签。

Here's a link to the official current WHATWG HTML Standard for enabling autocomplete. 这是用于启用自动完成功能的官方当前 WHATWG HTML 标准的链接。

Google wrote a pretty nice guide for developing web applications that are friendly for mobile devices. Google 编写了一份非常好的指南,用于开发对移动设备友好的 Web 应用程序。 They have a section on how to name the inputs on forms to easily use auto-fill.他们有一节介绍如何命名表单上的输入以轻松使用自动填充。 Eventhough it's written for mobile, this applies for both desktop and mobile!即使它是为移动设备编写的,这也适用于桌面设备和移动设备!

How to Enable AutoComplete on your HTML forms如何在 HTML 表单上启用自动完成

Here are some key points on how to enable autocomplete:以下是有关如何启用自动完成的一些要点:

  • Use a <label> for all your <input> fields对所有<input>字段使用<label>
  • Add a autocomplete attribute to your <input> tags and fill it in using this guide .autocomplete属性添加到您的<input>标签并使用本指南填写。
  • Name your name and autocomplete attributes correctly for all <input> tags为所有<input>标签正确命名您的nameautocomplete属性
  • Example :示例

     <label for="frmNameA">Name</label> <input type="text" name="name" id="frmNameA" placeholder="Full name" required autocomplete="name"> <label for="frmEmailA">Email</label> <input type="email" name="email" id="frmEmailA" placeholder="name@example.com" required autocomplete="email"> <!-- note that "emailC" will not be autocompleted --> <label for="frmEmailC">Confirm Email</label> <input type="email" name="emailC" id="frmEmailC" placeholder="name@example.com" required autocomplete="email"> <label for="frmPhoneNumA">Phone</label> <input type="tel" name="phone" id="frmPhoneNumA" placeholder="+1-555-555-1212" required autocomplete="tel">

How to name your <input> tags如何命名你的<input>标签

In order to trigger autocomplete, make sure you correctly name the name and autocomplete attributes in your <input> tags.为了触发自动完成,请确保在<input>标签中正确命名nameautocomplete属性。 This will automatically allow for autocomplete on forms.这将自动允许在表单上自动完成。 Make sure also to have a <label> !确保也有一个<label> This information can also be found here .此信息也可以在此处找到。

Here's how to name your inputs:以下是命名输入的方法:

  • Name名称
    • Use any of these for name : name fname mname lname使用其中任何一个作为namename fname mname lname
    • Use any of these for autocomplete :使用这些中的任何一个进行autocomplete
      • name (for full name) name (全名)
      • given-name (for first name) given-name (名字)
      • additional-name (for middle name) additional-name (中间名)
      • family-name (for last name) family-name (用于姓氏)
    • Example: <input type="text" name="fname" autocomplete="given-name">示例: <input type="text" name="fname" autocomplete="given-name">
  • Email电子邮件
    • Use any of these for name : email使用这些name任何一个: email
    • Use any of these for autocomplete : email使用其中任何一个进行autocompleteemail
    • Example: <input type="text" name="email" autocomplete="email">示例: <input type="text" name="email" autocomplete="email">
  • Address地址
    • Use any of these for name : address city region province state zip zip2 postal country使用其中任何一个作为nameaddress city region province state zip zip2 postal country
    • Use any of these for autocomplete :使用这些中的任何一个进行autocomplete
      • For one address input:对于一个地址输入:
        • street-address
      • For two address inputs:对于两个地址输入:
        • address-line1
        • address-line2
      • address-level1 (state or province) address-level1 (州或省)
      • address-level2 (city) address-level2 (城市)
      • postal-code (zip code) postal-code (邮政编码)
      • country
  • Phone电话
    • Use any of these for name : phone mobile country-code area-code exchange suffix ext使用其中任何一个作为namephone mobile country-code area-code exchange suffix ext
    • Use any of these for autocomplete : tel使用其中任何一个进行autocompletetel
  • Credit Card信用卡
    • Use any of these for name : ccname cardnumber cvc ccmonth ccyear exp-date card-type使用以下任何一个nameccname cardnumber cvc ccmonth ccyear exp-date card-type
    • Use any of these for autocomplete :使用这些中的任何一个进行autocomplete
      • cc-name
      • cc-number
      • cc-csc
      • cc-exp-month
      • cc-exp-year
      • cc-exp
      • cc-type
  • Usernames用户名
    • Use any of these for name : username使用这些name任何一个: username
    • Use any of these for autocomplete : username使用这些中的任何一个进行autocompleteusername
  • Passwords密码
    • Use any of these for name : password使用其中任何一个作为namepassword
    • Use any of these for autocomplete :使用这些中的任何一个进行autocomplete
      • current-password (for sign-in forms) current-password (用于登录表单)
      • new-password (for sign-up and password-change forms) new-password (用于注册和密码更改表单)

Resources资源

There is no cross browser way to do this, either the browser supports HTML5 and autofill, or it does'nt.没有跨浏览器的方法可以做到这一点,浏览器要么支持 HTML5 和自动填充,要么不支持。

All you have to do to make it work, is use the correct names for the inputs, and the browser will handle the rest, and you have no access to whatever data the browser has stored.要使其工作,您所要做的就是为输入使用正确的名称,浏览器将处理其余的工作,您无法访问浏览器存储的任何数据。

<input type="text" name="fname" /> //first name
<input type="text" name="lname" /> //last name
<input type="text" name="email" /> //email
//etc...

FIDDLE小提琴

According to this answer , the regular expressions used by Chrome are as follows :根据this answer ,Chrome使用的正则表达式如下:

  • first name: "first.*name|initials|fname|first$"名字: “first.*name|姓名首字母|fname|first$”
  • email: "e.?mail"电子邮件: “e.?mail”
  • address (line 1): "address.*line|address1|addr1|street"地址(第 1 行): “address.*line|address1|addr1|street”
  • zipcode: "zip|postal|post.*code|pcode|^1z$"邮政编码: “zip|postal|post.*code|pcode|^1z$”

As long as your names can be matched by these regular expressions, the autofill feature will work in most browsers.只要您的姓名可以与这些正则表达式匹配,自动填充功能就可以在大多数浏览器中使用。

you can add autocomplete for form or each form fields.您可以为表单或每个表单字段添加自动完成功能。 try this试试这个

<form autocomplete="on">
<input type="text" name="foo" />
....

</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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