如何在 Google Chrome 中触发自动填充?
我想知道是否有某种特殊标记可以为特定表单启用 Chrome 自动填充功能。我只发现有关如何禁用它的问题,但我想知道是否可以在 html 代码中添加某种标记,以便告诉浏览器“这是地址的输入”或“这是邮政编码”字段”以正确填写(假设用户激活了此功能)。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
Google 现在提供了相关文档:
帮助用户更快地结账自动填充 |网页 | Google Developers
和
创造令人惊叹的 作品表单:使用元数据启用自动完成 |
网页 |谷歌开发者
Google now provides documentation for this:
Help users checkout faster with Autofill | Web | Google Developers
and
Create Amazing Forms: Use metadata to enable auto-complete |
Web | Google Developers
就我而言,
$('#EmailAddress').attr('autocomplete', 'off');
不起作用。但以下内容适用于 jQuery 的 chrome 版本 67。
In my case,
$('#EmailAddress').attr('autocomplete', 'off');
is not worked.But following works on chrome Version 67 by jQuery.
2017 年更新: 看起来凯蒂的答案比我的有更多最新信息。未来的读者:请为她的答案投上一票。
这是一个很好的问题,也是一个需要文档的问题出乎意料的是很难获得。实际上,在很多情况下,您会发现 Chrome 自动填充功能“正常工作”。例如,以下 html 片段会生成一个表单,至少对我来说(Chrome v.18),在单击第一个字段后会自动填充该表单:
但是,这个答案并不令人满意,因为它将解决方案留在了以下领域: “魔法。”深入挖掘后,我了解到 Chrome(和其他支持自动填充的浏览器)主要依靠上下文线索来确定应填充到表单元素中的数据类型。此类上下文线索的示例包括输入元素的名称、元素周围的文本以及任何占位符文本。
然而最近,Chrome 团队承认这是一个不能令人满意的解决方案,他们已经开始推动此事的标准化。来自 Google 的内容非常丰富的帖子网站管理员小组最近讨论了这个问题,解释道:
(他们引用的“标准”是 Avalanchis 中提到的最新版本的规范 ' 上面的答案。)
谷歌的帖子继续描述了他们提出的解决方案(该解决方案遭到了帖子评论中的严重批评)。他们建议为此目的使用一个新属性:
。 ..其中
x-
代表“实验性”,如果 & 将被删除当这成为一个标准时。如需了解更多详情,请阅读这篇文章,或者如果您想深入挖掘,您可以在 whatwg 上找到该提案的更完整解释维基百科。更新:
正如这些富有洞察力中指出的答案,Chrome用来识别/识别公共字段的所有正则表达式都可以在
autofill_regex_constants.cc.utf8
。因此,要回答最初的问题,只需确保 html 字段的名称与这些表达式匹配即可。一些示例包括:“first.*name|initials|fname|first$”
“last。 *姓名|lname|姓氏|姓氏$|第二名|家庭。*姓名"
"e.?mail"
“地址.*line|address1|addr1|街道”
“zip|postal|post.*code|pcode|^1z$”
UPDATE for 2017: Looks like the answer from Katie has more up-to-date information than mine. Future readers: give your up-votes to her answer.
This is a great question and one for which documentation is surprisingly hard to come by. Actually, in many cases you will find that the Chrome Autofill functionality "just works." For example, the following snippet of html produces a form which, at least for me (Chrome v. 18), is automatically filled after clicking in the first field:
However, this answer is unsatisfactory, as it leaves the solution in the realm of "magic." Digging deeper I learned that Chrome (and other autofill-enabled browsers) primarily rely on contextual clues to determine the type of data that should be filled into form elements. Examples of such contextual clues include the
name
of an input element, the text surrounding the element, and any placeholder text.Recently, however, the Chrome team acknowledged that this is an unsatisfactory solution, and they have begun pressing for standardization in this matter. A very informative post from the Google Webmasters group recently discussed this issue, explaining:
(The "standards" they refer to is a more recent verion of the spec mentioned in Avalanchis' answer above.)
The Google post goes on to describe their proposed solution (which is met by significant criticism in the comments of the post). They propose the use of a new attribute for this purpose:
...where the
x-
stands for "experimental" and will be removed if & when this becomes a standard. Read the post for more details, or if you want to dig deeper, you will find a more complete explanation of the proposal on the whatwg wiki.UPDATE:
As pointed out in these insightful answers, all the regular expressions Chrome uses to identify/recognize common fields can be found in
autofill_regex_constants.cc.utf8
. So to answer the original question, just make sure the names for your html fields get matched by these expressions. Some examples include:"first.*name|initials|fname|first$"
"last.*name|lname|surname|last$|secondname|family.*name"
"e.?mail"
"address.*line|address1|addr1|street"
"zip|postal|post.*code|pcode|^1z$"
这个问题很老了,但我有一个更新的答案!
以下是用于启用自动完成功能的 WHATWG 文档的链接。
Google 编写了一个 非常好的指南,用于开发对移动设备友好的 Web 应用程序。他们有一个部分介绍如何命名表单上的输入以轻松使用自动填充功能。尽管它是为移动设备编写的,但这适用于桌面和移动设备!
如何在 HTML 表单上启用自动完成功能
以下是有关如何启用自动完成功能的一些要点:
字段使用
autocomplete
属性添加到您的标记中,并使用此 指南。
标记正确命名
name
和autocomplete
属性示例:
如何命名
标签
为了触发自动完成,请确保您正确命名在
标记中命名
name
和autocomplete
属性。这将自动允许表单上的自动完成。确保还有一个!您还可以在此处找到此信息。
以下是如何命名您的输入:
名称
:name fname mname lname
自动完成
:姓名
(全名)given-name
(名字)additional-name
(中间名)family-name
(姓氏)名称
:电子邮件
自动完成
:电子邮件
名称
:地址 城市 地区 省 州 zip zip2 邮政国家
自动完成
:街道地址
地址行1
地址行2
address-level1
(州或省)地址级别2
(城市)邮政编码
(邮政编码)国家
名称
:电话、手机、国家/地区代码、区号交换后缀分机号
自动完成
:tel
姓名
:ccname 卡号 cvc ccmonth ccyear exp-date 卡类型
自动完成
:抄送名称
抄送号码
cc-csc
cc-exp-month
cc-exp-year
cc-exp
cc 类型
名称
:用户名
自动完成
:用户名
名称
:密码
自动完成
:当前密码
(用于登录表单)新密码
(用于注册表单和密码更改表单)资源
This question is pretty old but I have an updated answer!
Here's a link to the WHATWG documentation for enabling autocomplete.
Google wrote a pretty nice guide for developing web applications that are friendly for mobile devices. 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
Here are some key points on how to enable autocomplete:
<label>
for all your<input>
fieldsautocomplete
attribute to your<input>
tags and fill it in using this guide.name
andautocomplete
attributes correctly for all<input>
tagsExample:
How to name your
<input>
tagsIn order to trigger autocomplete, make sure you correctly name the
name
andautocomplete
attributes in your<input>
tags. This will automatically allow for autocomplete on forms. Make sure also to have a<label>
! This information can also be found here.Here's how to name your inputs:
name
:name fname mname lname
autocomplete
:name
(for full name)given-name
(for first name)additional-name
(for middle name)family-name
(for last name)<input type="text" name="fname" autocomplete="given-name">
name
:email
autocomplete
:email
<input type="text" name="email" autocomplete="email">
name
:address city region province state zip zip2 postal country
autocomplete
:street-address
address-line1
address-line2
address-level1
(state or province)address-level2
(city)postal-code
(zip code)country
name
:phone mobile country-code area-code exchange suffix ext
autocomplete
:tel
name
:ccname cardnumber cvc ccmonth ccyear exp-date card-type
autocomplete
:cc-name
cc-number
cc-csc
cc-exp-month
cc-exp-year
cc-exp
cc-type
name
:username
autocomplete
:username
name
:password
autocomplete
:current-password
(for sign-in forms)new-password
(for sign-up and password-change forms)Resources
根据我的测试,
x-autocomplete
标记没有执行任何操作。而是在输入标记上使用autocomplete
标记,并根据此处的 HTML 规范设置它们的值 http://www.whatwg。 org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#autofill-field 。示例:
父表单标签需要 autocomplete="on" 和 method="POST"。
From my testing, the
x-autocomplete
tag does nothing. Instead useautocomplete
tags on your input tags, and set their values according to the HTML spec here http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#autofill-field .Example:
The parent form tag needs autocomplete="on" and method="POST".
您需要适当地命名元素,以便浏览器自动填充它们。
以下是 IETF 规范:
http://www.ietf.org/rfc/rfc3106.txt< /a>1
You need to name the elements appropriately so that the browser will autofill them.
Here's the IETF spec for this:
http://www.ietf.org/rfc/rfc3106.txt1
我刚刚研究了规范并得到了一个很好的工作示例 - 包括更多字段。
JSBIN
它包含 2 个独立的地址区域以及不同的地址类型。
在 iOS 8.1.0 上也进行了测试,它似乎总是一次填充所有字段,而桌面 chrome 会自动填充地址。
I just played arround with the spec and got a nice working example - including a few more fields.
JSBIN
It contains 2 separate address areas and also differen address-types.
Tested it also on iOS 8.1.0 and it seems that it always fill all fields at once, while desktop chrome autofill address by address.
对此自动填充功能的更多控制,Chrome Canary 推出了一个新的实验性 API,可用于在询问用户后访问数据:
看起来我们将获得 chromium.org/developers/using-requestautocomplete" rel="nofollow">http://www.chromium.org/developers/using-requestautocomplete
http://blog.alexmaccaw.com/requestautocomplete
Google 提供的新信息:
http://googlewebmastercentral.blogspot.de/2015/03/helping-users-fill-out-online-forms.html
https://developers.google.com/web/fundamentals/input/form/label-and-name-inputs#use-metadata-to-enable-auto-complete
Looks like we'll get more control about this autofill feature, there is a new experimental API coming to Chrome Canary, which can be used to access the data after asking the user for it:
http://www.chromium.org/developers/using-requestautocomplete
http://blog.alexmaccaw.com/requestautocomplete
new infos by google:
http://googlewebmastercentral.blogspot.de/2015/03/helping-users-fill-out-online-forms.html
https://developers.google.com/web/fundamentals/input/form/label-and-name-inputs#use-metadata-to-enable-auto-complete
这是真正的答案:
这有效:http://jsfiddle.net/68LsL1sq/1/< /a>
这不是:http://jsfiddle.net/68LsL1sq/2/
唯一的区别在于标签本身。 “Nom”来自葡萄牙语中的“Name”或“Nome”。
所以这就是您需要的:
仅此而已。
Here it is the real answer:
This works: http://jsfiddle.net/68LsL1sq/1/
<label for="name">Nom</label>
This not: http://jsfiddle.net/68LsL1sq/2/
<label for="name">No</label>
The only difference is in the label itself. The "Nom" cames from "Name" or "Nome" in portuguese.
So here is what you need:
<label for="id_of_field">Name</label>
<input id="id_of_field"></input>
Nothing more.
这是 Google 自动填充“名称”的新列表。有任何允许的语言的所有受支持的名称。
autofill_regex_constants.cc
Here is the new list of Google Autofill "names". There are all the supported names in any allowed language.
autofill_regex_constants.cc