我创建了一个使用标签框下拉的web应用程序。除了Chrome浏览器(Version 21.0.1180.89),这在所有浏览器中都很有效。
尽管输入字段和表单字段都有autocomplete="off"属性,Chrome坚持显示该字段以前条目的下拉历史,这是删除标签框列表。
我创建了一个使用标签框下拉的web应用程序。除了Chrome浏览器(Version 21.0.1180.89),这在所有浏览器中都很有效。
尽管输入字段和表单字段都有autocomplete="off"属性,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 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属性的事情,比如根据元素名称使用选择器,这个解决方案应该是无害的。
防止用户名(或电子邮件)和密码自动完成:
<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 +
更新
现在Chrome似乎忽略了style="display: none;"或style="visibility: hidden; "属性。
你可以把它改成这样:
<input style="opacity: 0;position: absolute;">
<input type="password" style="opacity: 0;position: absolute;">
根据我的经验,Chrome只自动完成第一个<input type="password">和前一个<input>。所以我添加了:
<input style="display:none">
<input type="password" style="display:none">
到<form>的顶部,该情况得到解决。
我发布这个答案是为了给这个问题带来一个更新的解决方案。 我目前使用的是Chrome 49,没有给出的答案。 我也在寻找一个解决方案与其他浏览器和以前的版本。
把这些代码放在表单的开头
<div style="display: none;">
<input type="text" autocomplete="new-password">
<input type="password" autocomplete="new-password">
</div>
然后,对于您的真实密码字段,使用
<input type="password" name="password" autocomplete="new-password">
如果这不再工作,或如果您遇到其他浏览器或版本的问题,请注释此答案。
批准:
Chrome浏览器:49 Firefox: 44,45 边缘:25 Internet Explorer: 11
将输入类型属性更改为type="search"。
谷歌不会对输入的搜索类型应用自动填充。