我遇到了chrome自动填充行为的几个形式的问题。

表单中的字段都有非常常见和准确的名称,例如“email”、“name”或“password”,并且它们还设置了autocomplete=“off”。

自动完成标志已经成功禁用了自动完成行为,当你开始输入时,会出现一个下拉的值,但没有改变Chrome自动填充字段的值。

这种行为是可以的,除了chrome填充输入不正确,例如填充电话输入与电子邮件地址。客户抱怨过这个问题,所以它被证实在很多情况下都发生了,而不是我在我的机器上本地操作的某种结果。

目前我能想到的唯一解决方案是动态生成自定义输入名称,然后在后端提取值,但这似乎是一种相当笨拙的解决这个问题的方法。是否有任何标签或怪癖,改变自动填充行为,可以用来解决这个问题?


当前回答

以下是我提出的解决方案,因为谷歌坚持推翻人们似乎做出的每一个变通办法。

选项1 -选择所有文本点击

为你的用户设置输入的值为一个例子(例如your@email.com),或者字段的标签(例如Email),并在你的输入中添加一个名为focus-select的类:

<input type="text" name="email" class="focus-select" value="your@email.com">
<input type="password" name="password" class="focus-select" value="password">

下面是jQuery:

$(document).on('click', '.focus-select', function(){
  $(this).select();
});

我真的看不出Chrome会搞砸数值。那太疯狂了。希望这是一个安全的解决方案。

选项2 -设置电子邮件值为一个空间,然后删除它

假设您有两个输入,例如电子邮件和密码,将电子邮件字段的值设置为“”(一个空格),并添加属性/值autocomplete=“off”,然后使用JavaScript清除此设置。您可以将密码值保留为空。

如果用户出于某种原因没有JavaScript,请确保您在服务器端修改了他们的输入(无论如何您都应该这样做),以防他们不删除空格。

下面是jQuery:

$(document).ready(function() {
  setTimeout(function(){
    $('[autocomplete=off]').val('');
  }, 15);
});

我将超时设置为15,因为5似乎在我的测试中偶尔可以工作,所以将这个数字增加三倍似乎是一个安全的赌注。

如果没有将初始值设置为空格,Chrome会让输入框显示为黄色,就好像它已经自动填充了一样。

选项3 -隐藏输入

把这个放在表单的开头:

<!-- Avoid Chrome autofill -->
<input name="email" class="hide">

CSS:

.hide{ display:none; }

确保您保留了HTML注释,这样其他开发人员就不会删除它!还要确保隐藏输入的名称是相关的。

其他回答

1月20日chrome更新

上面的解决方案都不起作用,包括添加假字段或设置autocomplete=new-password。是的,这些chrome浏览器不会自动完成,但当你输入密码字段时,它会再次建议密码。

我发现,如果你删除密码字段,然后添加它再次没有id,然后chrome不会自动填充它,也不建议输入密码。

使用类来获取密码值而不是id。

对于firefox来说,仍然需要添加一个虚拟元素。

这个解决方案还允许/禁止基于标志的自动补全:

html:

<!-- form is initially hidden, password field has a dummy id and a class 'id' -->
<form id="loginForm" style="display:none;">
   <span>email:</span><input type="text" placeholder="Email" id="loginEmailInputID"/>
   <span>Password:</span><input class="loginPasswordInput" type="password" placeholder="Password" id="justForAutocomplete"/>
</form>

页面加载:

function hideAutoComplete(formId) {
    let passwordElem=$('#'+formId+' input:password'), prev=passwordElem.prev();
    passwordElem.remove();
    prev.after($('<input type="password" style="display:none">'), passwordElem.clone().val('').removeAttr('id'));
}

if (!allowAutoComplete) hideAutoComplete('loginForm');
$('#loginForm').show();

当你需要密码时:

$('.loginPasswordInput').val();

为用户名字段输入值' '(空格)。

<input type = 'text' value = ' ' name = 'username' />

如果您曾经用用户输入的值填充用户名,如果没有用户输入的值,则编码输入' '。

编辑:我还不得不改变'用户名'字段有一个其他的名字,而不是'用户名',例如。“nameofuser”

有时即使autocomplete=off也不能防止在错误的字段中填写凭据。

一个解决方法是禁用浏览器自动填充使用只读模式,并设置可写焦点:

 <input type="password" readonly onfocus="this.removeAttribute('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

解释:浏览器自动填充凭据错误的文本字段?

错误地填充输入,例如用电子邮件地址填充电话输入

有时我在Chrome和Safari浏览器上注意到这种奇怪的行为,当密码字段以相同的形式出现时。我猜,浏览器会寻找一个密码字段来插入您保存的凭证。然后它自动将用户名填充到最近的textlike输入字段,在DOM中出现在密码字段之前(只是由于观察而猜测)。由于浏览器是最后一个实例,你无法控制它,

上面的这个即时修复对我来说是有效的。

2022年4月:autocomplete="off"仍然不能在Chrome中工作,我不相信在查看了与此问题相关的Chromium bug后(可能只针对密码字段)。我看到2014年报告的问题被关闭为“WontFix”,问题仍然悬而未决,正在讨论[1][2]。据我所知,Chromium团队不相信有一个有效的用例autocomplete="off"。

总的来说,我仍然认为这两种极端策略(“总是尊重自动完成=关闭”和“从不尊重自动完成=关闭”)都是不好的。

https://bugs.chromium.org/p/chromium/issues/detail?id=914451#c66

他们的印象是,网站不会正确使用它,并决定不应用它,提出以下建议:

In cases where you want to disable autofill, our suggestion is to utilize the autocomplete attribute to give semantic meaning to your fields. If we encounter an autocomplete attribute that we don't recognize, we won't try and fill it. As an example, if you have an address input field in your CRM tool that you don't want Chrome to Autofill, you can give it semantic meaning that makes sense relative to what you're asking for: e.g. autocomplete="new-user-street-address". If Chrome encounters that, it won't try and autofill the field.

https://bugs.chromium.org/p/chromium/issues/detail?id=587466#c10

虽然这个“建议”目前对我来说是可行的,但它可能并不总是正确的,看起来团队正在进行实验,这意味着自动完成功能可能会在新版本中改变。

我们不得不求助于这个是愚蠢的,但唯一可靠的方法是尽可能地迷惑浏览器:

在不向浏览器泄露任何信息的情况下为输入命名,例如id="field1"而不是id="country"。 设置autocomplete="do-not-autofill",基本上使用任何不会让浏览器识别为可自动填充字段的值。


2021年1月:autocomplete="off"现在按预期工作(在Chrome 88 macOS上测试)。

要做到这一点,请确保input标记位于Form标记中


2020年9月:autocomplete=" Chrome -off"禁用Chrome自动填充功能。


原答案,2015年:

对于新版本的Chrome浏览器,你只需要在密码栏中输入autocomplete="new-password"就可以了。我检查过了,工作正常。

在这次讨论中,我从Chrome开发者那里得到了一个建议: https://bugs.chromium.org/p/chromium/issues/detail?id=370363#c7

P.S. Note that Chrome will attempt to infer autofill behavior from name, id and any text content it can get surrounding the field including labels and arbitrary text nodes. If there is a autocomplete token like street-address in context, Chrome will autofill that as such. The heuristic can be quite confusing as it sometimes only trigger if there are additional fields in the form, or not if there are too few fields in the form. Also note that autocomplete="no" will appear to work but autocomplete="off" will not for historical reasons. autocomplete="no" is you telling the browser that this field should be auto completed as a field called "no". If you generate unique random autocomplete names you disable auto complete.

如果你的用户访问了不好的表单,他们的自动填充信息可能会被损坏。让他们手动进入并修复Chrome中的自动填充信息可能是他们采取的必要行动。

以前输入的值缓存chrome显示为下拉选择列表。这可以被autocomplete=off禁用,在chrome的高级设置中显式保存地址,当地址字段获得焦点时,会自动填充弹出窗口。这可以通过autocomplete="false"来禁用。但它将允许chrome下拉显示缓存值。

在一个输入html字段下面将关闭两者。

Role="presentation" & autocomplete="off"

在选择地址自动填充的输入字段时,Chrome忽略了那些没有前面标签html元素的输入字段。

为了确保chrome解析器忽略自动填充地址弹出的输入字段,可以在标签和文本框之间添加隐藏按钮或图像控件。这将打破自动填充的标签输入对创建的chrome解析序列。 在解析地址字段时忽略复选框

Chrome也考虑标签元素上的“for”属性。它可以用来打破chrome的解析顺序。