我创建了一个使用标签框下拉的web应用程序。除了Chrome浏览器(Version 21.0.1180.89),这在所有浏览器中都很有效。

尽管输入字段和表单字段都有autocomplete="off"属性,Chrome坚持显示该字段以前条目的下拉历史,这是删除标签框列表。


当前回答

一些2020年底更新。我尝试了不同网站上的所有老方法。没有一个成功!: - ( 然后我发现了这个: 使用

<input type="search"/> 

自动补全功能消失了!

Chrome 86, FireFox, Edge 87都成功了。

其他回答

我刚刚尝试了下面的方法,它似乎在Chrome 53上起了作用——当输入密码字段时,它还禁用了“使用密码用于:”下拉菜单。

简单地设置你的密码输入类型为文本,然后添加onfocus处理程序(内联或通过jQuery/vanilla JS)来设置类型为密码:

onfocus="this.setAttribute('type','password')"

或者更好:

onfocus="if(this.getAttribute('type')==='text') this.setAttribute('type','password')"

我最近遇到了这个问题,没有一个答案对我有用。在我的情况下,因为我不关心输入字段内嵌套的“形式”标签,我通过提供一个空的数据表输入修复chrome自动补全问题。所以现在chrome应该为你提供自动完成建议从“数据列表”是空的。请记住,如果输入嵌套在“form”标记中,此解决方案将不起作用。令人惊讶的是,除了这个技巧,其他什么都不管用。

<input type="text" autocomplete="off" list="emptyList" />
<datalist id="emptyList"></datalist>

你可以在这里了解更多关于数据列表的信息: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/datalist

考虑到浏览器兼容性,使用它似乎是安全的。

浏览器不关心自动完成=关闭自动,甚至填充凭据到错误的文本字段?

我通过将密码字段设置为只读并激活它来修复它,当用户单击进入它或使用tab键到该字段时。

修复浏览器自动填充:只读和设置可写的焦点(在鼠标点击和标签通过字段)

 <input type="password" readonly  
     onfocus="$(this).removeAttr('readonly');"/>

更新: Mobile Safari在字段中设置光标,但不显示虚拟键盘。新的修复工作像以前一样,但处理虚拟键盘:

<input id="email" readonly type="email" onfocus="if (this.hasAttribute('readonly')) {
    this.removeAttribute('readonly');
    // fix for mobile safari to show virtual keyboard
    this.blur();    this.focus();  }" />

现场演示https://jsfiddle.net/danielsuess/n0scguv6/

/ / UpdateEnd

顺便说一下,更多关于我观察到的信息:

Sometimes I notice this strange behavior on Chrome and Safari, when there are password fields in the same form. I guess, the browser looks for a password field to insert your saved credentials. Then it autofills username into the nearest textlike-input field , that appears prior the password field in DOM (just guessing due to observation). As the browser is the last instance and you can not control it, sometimes even autocomplete=off would not prevent to fill in credentials into wrong fields, but not user or nickname field.

好吧,有点晚了,但似乎有一点关于自动补全应该和不应该工作的误解。根据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 48+使用这个解决方案:

Put fake fields before real fields: <form autocomplete="off"> <input name="fake_email" class="visually-hidden" type="text"> <input name="fake_password" class="visually-hidden" type="password"> <input autocomplete="off" name="email" type="text"> <input autocomplete="off" name="password" type="password"> </form> Hide fake fields: .visually-hidden { margin: -1px; padding: 0; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); clip: rect(0, 0, 0, 0); position: absolute; } You did it!

同样,这也适用于旧版本。