我创建了一个使用标签框下拉的web应用程序。除了Chrome浏览器(Version 21.0.1180.89),这在所有浏览器中都很有效。
尽管输入字段和表单字段都有autocomplete="off"属性,Chrome坚持显示该字段以前条目的下拉历史,这是删除标签框列表。
我创建了一个使用标签框下拉的web应用程序。除了Chrome浏览器(Version 21.0.1180.89),这在所有浏览器中都很有效。
尽管输入字段和表单字段都有autocomplete="off"属性,Chrome坚持显示该字段以前条目的下拉历史,这是删除标签框列表。
当前回答
为了防止自动补全,只需设置一个空白作为输入值:
<input type="text" name="name" value=" ">
其他回答
[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
截至Chrome 42,这个线程中的解决方案/hacks(截至2015-05-21T12:50:23+00:00)都不能禁用单个字段或整个表单的自动完成。
编辑:我发现你实际上只需要在其他字段之前插入一个虚假的电子邮件字段到你的表单(你可以用display: none隐藏它),以防止自动完成。我假设chrome存储某种形式的签名与每个自动完成字段,包括另一个电子邮件字段破坏这个签名,防止自动完成。
<form action="/login" method="post">
<input type="email" name="fake_email" style="display:none" aria-hidden="true">
<input type="email" name="email">
<input type="password" name="password">
<input type="submit">
</form>
好消息是,因为“表单签名”被破坏了,所以没有一个字段是自动完成的,所以在提交之前不需要JS来清除假字段。
旧的回答:
我发现唯一可行的方法是在真正的字段之前插入两个电子邮件和密码类型的虚拟字段。你可以将它们设置为display: none来隐藏它们(忽略这些字段是不够聪明的):
<form action="/login" method="post">
<input type="email" name="fake_email" style="display:none" aria-hidden="true">
<input type="password" name="fake_password" style="display:none" aria-hidden="true">
<input type="email" name="email">
<input type="password" name="password">
<input type="submit">
</form>
不幸的是,字段必须在表单中(否则两组输入都是自动填充的)。因此,为了真正忽略假字段,你需要在form submit上运行一些JS来清除它们:
form.addEventListener('submit', function() {
form.elements['fake_email'].value = '';
form.elements['fake_password'].value = '';
});
从上面可以注意到,用Javascript清除值可以覆盖自动完成。因此,如果失去适当的行为与JS禁用是可以接受的,你可以简化这一切与JS自动完成“polyfill”Chrome:
(function(document) {
function polyfillAutocomplete(nodes) {
for(var i = 0, length = nodes.length; i < length; i++) {
if(nodes[i].getAttribute('autocomplete') === 'off') {
nodes[i].value = '';
}
}
}
setTimeout(function() {
polyfillAutocomplete(document.getElementsByTagName('input'));
polyfillAutocomplete(document.getElementsByTagName('textarea'));
}, 1);
})(window.document);
你可以使用下面的概念来实现AutoComplete='false' chrome以及其他浏览器。取一个虚拟输入类型,它的不透明度为0。默认chrome浏览器已经触发第一个已经隐藏。
<input style="opacity: 0; position: absolute; z-index: -1;" name="email">
<input type="search" name="email" class="form-control" autocomplete="new-email" id="email">
对任何想要解决这个问题的人来说,我终于明白了。
Chrome只服从的自动完成="off"如果页面是HTML5页面(我使用XHTML)。
我把我的页面转换成HTML5,问题就消失了(捂脸)。
我把这种方法称为大锤方法,但它似乎在我尝试过的所有其他方法都失败的地方起了作用:
<input autocomplete="off" data-autocomplete-ninja="true" name="fa" id="fa" />
注意:输入名称和id属性不应该包含任何会给浏览器提示数据是什么的东西,否则这个解决方案将无法工作。例如,我使用“fa”而不是“FullAddress”。
和下面的脚本页面加载(这个脚本使用JQuery):
$("[data-autocomplete-ninja]").each(function () {
$(this).focus(function () {
$(this).data("ninja-name", $(this).attr("name")).attr("name", "");
}).blur(function () {
$(this).attr("name", $(this).data("ninja-name"));
});
});
上面的解决方案应该可以防止浏览器自动填充从其他表单收集的数据,或者从同一表单上以前提交的数据。
基本上,当输入处于焦点中时,我删除了name属性。只要在元素处于焦点时不做任何需要name属性的事情,比如根据元素名称使用选择器,这个解决方案应该是无害的。