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

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


当前回答

为了防止自动补全,只需设置一个空白作为输入值:

<input type="text" name="name" value="  ">

其他回答

TL;DR:告诉Chrome这是一个新的密码输入,它不会提供旧的密码作为自动补全建议:

<input type="password" name="password" autocomplete="new-password">

由于设计决定,Autocomplete ="off"不起作用——大量研究表明,如果用户可以将密码存储在浏览器或密码管理器中,他们就会有更长时间和更难破解密码。

自动完成的规范已经改变,现在支持各种值,使登录表单更容易自动完成:

<!-- Auto fills with the username for the site, even though it's email format -->
<input type="email" name="email" autocomplete="username">

<!-- current-password will populate for the matched username input  -->
<input type="password" autocomplete="current-password" />

如果你不提供这些Chrome浏览器仍然试图猜测,当它做它忽略autocomplete="off"。

解决方案是密码重置表单也存在自动完成值:

<label>Enter your old password:
    <input type="password" autocomplete="current-password" name="pass-old" />
</label>
<label>Enter your new password:
    <input type="password" autocomplete="new-password" name="pass-new" />
</label>
<label>Please repeat it to be sure:
    <input type="password" autocomplete="new-password" name="pass-repeat" />
</label>

你可以使用autocomplete="new-password"标志告诉Chrome浏览器不要猜测密码,即使它已经为这个网站存储了一个。

Chrome还可以直接使用凭据API管理网站的密码,这是一个标准,最终可能会得到普遍支持。

防止用户名(或电子邮件)和密码自动完成:

<input type="email" name="email"><!-- Can be type="text" -->
<input type="password" name="password" autocomplete="new-password">

防止自动完成字段(可能不起作用):

<input type="text" name="field" autocomplete="nope">

解释:

Autocomplete仍然适用于<input>,尽管有Autocomplete ="off",但你可以将off更改为随机字符串,如nope。


其他禁用字段自动补全的“解决方案”(这不是正确的方法,但它是有效的):

1.

HTML:

<input type="password" id="some_id" autocomplete="new-password">

JS (onload):

(function() {
    var some_id = document.getElementById('some_id');
    some_id.type = 'text';
    some_id.removeAttribute('autocomplete');
})();

或者使用jQuery:

$(document).ready(function() {
    var some_id = $('#some_id');
    some_id.prop('type', 'text');
    some_id.removeAttr('autocomplete');
});

2.

HTML:

<form id="form"></form>

JS (onload):

(function() {
    var input = document.createElement('INPUT');
    input.type = 'text';
    document.getElementById('form').appendChild(input);
})();

或者使用jQuery:

$(document).ready(function() {
    $('<input>', {
        type: 'text'
    }).appendTo($('#form'));
});

使用jQuery添加多个字段:

function addField(label) { var div = $('<div>'); var input = $('<input>', { type: 'text' }); if(label) { var label = $('<label>', { text: label }); label.append(input); div.append(label); } else { div.append(input); } div.appendTo($('#form')); } $(document).ready(function() { addField(); addField('Field 1: '); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="form"></form>


适用于:

铬:49 + Firefox: 44 +

好吧,有点晚了,但似乎有一点关于自动补全应该和不应该工作的误解。根据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 96, Opera 82测试

/* Change Autocomplete styles in Chrome*/
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
textarea:-webkit-autofill,
textarea:-webkit-autofill:hover,
textarea:-webkit-autofill:focus,
select:-webkit-autofill,
select:-webkit-autofill:hover,
select:-webkit-autofill:focus {
    border: none;
    border-bottom: 1px solid;
    -webkit-text-fill-color: #000;
    -webkit-box-shadow: 0 0 0 1000px transparent inset;
}

始终有效的解决方案

我用随机字符的使用解决了谷歌Chrome无休止的战斗。当你总是用随机字符串渲染自动补全时,它永远不会记住任何东西。

<input name="name" type="text" autocomplete="rutjfkde">

希望对其他人有所帮助。

2022年更新:

Chrome做了这个改进:autocomplete="new-password"这将解决它,但我不确定,如果Chrome在一段时间后再次更改为不同的功能。