我创建了一个使用标签框下拉的web应用程序。除了Chrome浏览器(Version 21.0.1180.89),这在所有浏览器中都很有效。
尽管输入字段和表单字段都有autocomplete="off"属性,Chrome坚持显示该字段以前条目的下拉历史,这是删除标签框列表。
我创建了一个使用标签框下拉的web应用程序。除了Chrome浏览器(Version 21.0.1180.89),这在所有浏览器中都很有效。
尽管输入字段和表单字段都有autocomplete="off"属性,Chrome坚持显示该字段以前条目的下拉历史,这是删除标签框列表。
当前回答
基本上我们可以摆脱任何文本框的自动完成从chrome, firefox或任何类型的浏览器。这是简单的javascript。
window.onload=function(){
setTimeout(function(){
document.getElementById('username').value = '';
document.getElementById('password').value = '';
},100);
}
当你的窗口完成加载,100毫秒后,我们的用户名和密码字段的值将被删除。 我认为这是最好的方法做自动完成在所有浏览器(特别是chrome)。
其他回答
我在我的表单中的一个搜索字段也有类似的问题。无论是autocomplete= "false"还是autocomplete= "off"都不适合我。结果是,当你有aria-label属性在输入元素是设置为一些像城市或地址或类似的东西,chrome覆盖你所有的设置和显示自动完成无论如何
所以修复我所做的是从aria标签中删除城市部分,并提出一个不同的值。最后自动完成停止显示
2021更新:将<input type="text">更改为<input type="search" autocomplete="off" >
仅此而已。保留下面的答案作为怀旧。
为了一个可靠的解决方案,你可以添加以下代码到你的布局页面:
<div style="display: none;">
<input type="text" id="PreventChromeAutocomplete"
name="PreventChromeAutocomplete" autocomplete="address-level4" />
</div>
Chrome尊重autocomplete=off只有当至少有一个其他输入元素的形式与任何其他自动完成值。
这对密码字段不起作用——这些在Chrome中的处理方式非常不同。详情见https://code.google.com/p/chromium/issues/detail?id=468153。
更新:Chromium团队于2016年3月11日关闭了“无法修复”的Bug。请参阅我最初提交的错误报告中的最后一条评论,以获得完整的解释。TL;DR:使用语义自动补全属性,如autocomplete="new-street-address",以避免Chrome执行自动填充。
快速破解,如果你读了上面的答案仍然得到自动补全,你可以试试这个。给出随机字符串将删除自动补全。
<input autocomplete="somerandomstring" or autocomplete="disabled">
我不知道这方法对不对,它就是对我有用。
好吧,有点晚了,但似乎有一点关于自动补全应该和不应该工作的误解。根据HTML规范,用户代理(在这种情况下Chrome)可以覆盖自动完成:
https://www.w3.org/TR/html5/forms.html#autofilling-form-controls:-the-autocomplete-attribute
A user agent may allow the user to override an element's autofill field name, e.g. to change it from "off" to "on" to allow values to be remembered and prefilled despite the page author's objections, or to always "off", never remembering values. However, user agents should not allow users to trivially override the autofill field name from "off" to "on" or other values, as there are significant security implications for the user if all values are always remembered, regardless of the site's preferences.
所以在Chrome的例子中,开发者基本上说“我们将把这个留给用户来决定他们是否想要自动完成工作。”如果你不想要它,就不要在浏览器中启用它”。
然而,在我看来,这对他们来说似乎有点过于热情了,但事实就是如此。规范还讨论了这样做的潜在安全影响:
The "off" keyword indicates either that the control's input data is particularly sensitive (for example the activation code for a nuclear weapon); or that it is a value that will never be reused (for example a one-time-key for a bank login) and the user will therefore have to explicitly enter the data each time, instead of being able to rely on the UA to prefill the value for him; or that the document provides its own autocomplete mechanism and does not want the user agent to provide autocompletion values.
所以,在经历了和其他人一样的挫折之后,我找到了一个适合我的解决方案。它类似于autocomplete="false"答案。
Mozilla的一篇文章正是针对这个问题:
https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion
在某些情况下,即使autocomplete属性被设置为关闭,浏览器也会继续建议自动补全值。这种出乎意料的行为可能会让开发人员感到困惑。真正强制不完成的技巧是为属性分配一个随机字符串
所以下面的代码应该可以工作:
autocomplete="nope"
以下每一项也应如此:
autocomplete="false"
autocomplete="foo"
autocomplete="bar"
我看到的问题是浏览器代理可能足够聪明,可以学习自动完成属性,并在下次看到表单时应用它。如果它确实这样做了,我所能看到的解决问题的唯一方法是在生成页面时动态更改自动完成属性值。
值得一提的一点是,许多浏览器会忽略登录字段(用户名和密码)的自动完成设置。正如Mozilla文章所述:
For this reason, many modern browsers do not support autocomplete="off" for login fields. If a site sets autocomplete="off" for a form, and the form includes username and password input fields, then the browser will still offer to remember this login, and if the user agrees, the browser will autofill those fields the next time the user visits this page. If a site sets autocomplete="off" for username and password input fields, then the browser will still offer to remember this login, and if the user agrees, the browser will autofill those fields the next time the user visits this page. This is the behavior in Firefox (since version 38), Google Chrome (since 34), and Internet Explorer (since version 11).
最后是关于属性是属于form元素还是input元素的一些信息。规范再次给出了答案:
如果省略autocomplete属性,则使用与元素表单所有者的autocomplete属性状态对应的默认值(“on”或“off”)。如果没有表单所有者,则使用值“on”。
所以。把它放在表单上应该适用于所有输入字段。将它放在单个元素上应该只应用于该元素(即使表单上没有)。如果完全没有设置autocomplete,则默认为on。
总结
禁用整个表单的自动完成功能:
<form autocomplete="off" ...>
或者如果你动态地需要这样做:
<form autocomplete="random-string" ...>
禁用单个元素的自动补全功能(无论是否存在表单设置)
<input autocomplete="off" ...>
或者如果你动态地需要这样做:
<input autocomplete="random-string" ...>
请记住,某些用户代理甚至可以覆盖您禁用自动补全的最艰难的尝试。
Chrome版本34现在忽略autocomplete=off, 看到这个。
很多关于这是好事还是坏事的讨论?你的观点是什么?