我创建了一个使用标签框下拉的web应用程序。除了Chrome浏览器(Version 21.0.1180.89),这在所有浏览器中都很有效。
尽管输入字段和表单字段都有autocomplete="off"属性,Chrome坚持显示该字段以前条目的下拉历史,这是删除标签框列表。
我创建了一个使用标签框下拉的web应用程序。除了Chrome浏览器(Version 21.0.1180.89),这在所有浏览器中都很有效。
尽管输入字段和表单字段都有autocomplete="off"属性,Chrome坚持显示该字段以前条目的下拉历史,这是删除标签框列表。
当前回答
对于这个问题,我使用了这个css解决方案。这对我很有用。
input{
text-security:disc !important;
-webkit-text-security:disc !important;
-moz-text-security:disc !important;
}
其他回答
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管理网站的密码,这是一个标准,最终可能会得到普遍支持。
我提出了以下解决方案,查询所有字段的属性autocomplple ="off",然后将其值设置为一个单独的空间,然后设置一个约200ms的定时器,并将值设置回一个空字符串。
例子:
// hack to prevent auto fill on chrome
var noFill = document.querySelectorAll("input[autocomplete=off]");
noFill.forEach(function(el) {
el.setAttribute("value", " ");
setTimeout(function() {
el.setAttribute("value", "");
}, 200);
});
我选择200毫秒作为计时器,因为经过一些实验,200毫秒似乎是我的电脑上chrome放弃尝试自动完成字段所需的时间。我很乐意听到其他时间对其他人来说似乎更好。
我发现这个解决方案是最合适的:
function clearChromeAutocomplete()
{
// not possible, let's try:
if (navigator.userAgent.toLowerCase().indexOf('chrome') >= 0)
{
document.getElementById('adminForm').setAttribute('autocomplete', 'off');
setTimeout(function () {
document.getElementById('adminForm').setAttribute('autocomplete', 'on');
}, 1500);
}
}
它必须在dom就绪后加载,或者在表单呈现后加载。
防止用户名(或电子邮件)和密码自动完成:
<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 +
[2021年适用于Chrome(v88, 89, 90), Firefox, Brave, Safari] 旧的答案已经写在这里,将适用于试验和错误,但大多数 他们没有链接到任何官方文件或什么Chrome不得不说这一点 的事。
问题中提到的问题是因为Chrome的自动填充功能,这里是Chrome在这个bug链接中的立场- https://bugs.chromium.org/p/chromium/issues/detail?id=468153#c164
简单地说,有两种情况-
[CASE 1]: Your input type is something other than password. In this case, the solution is simple, and has three steps. Add name attribute to input name should not start with a value like email or username, otherwise Chrome still ends up showing the dropdown. For example, name="emailToDelete" shows the dropdown, but name="to-delete-email" doesn't. Same applies for autocomplete attribute. Add autocomplete attribute, and add a value which is meaningful for you, like new-field-name It will look like this, and you won't see the autofill for this input again for the rest of your life - <input type="text/number/something-other-than-password" name="x-field-1" autocomplete="new-field-1" /> [CASE 2]: input type is password Well, in this case, irrespective of your trials, Chrome will show you the dropdown to manage passwords / use an already existing password. Firefox will also do something similar, and same will be the case with all other major browsers. [1] In this case, if you really want to stop the user from seeing the dropdown to manage passwords / see a securely generated password, you will have to play around with JS to switch input type, as mentioned in the other answers of this question.
[1]关于关闭自动补全的详细MDN文档- https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion