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

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

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

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

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


当前回答

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();

其他回答

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

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

Role="presentation" & autocomplete="off"

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

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

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

What I have done it to change the input type="text" to a multi line input ie. overflow-x:hidden; overflow-y:hidden; vertical-align:middle; resize: none; A quick explanation of the code: The overflow-x and -y hidden wil disable the scroll buttons on the right of the textarea box. The vertial algin will align the lable vertical middle with the text area and the resize: none will disable the resize grabber at the bottom right of the textarea. In essance it means that your textarea will appear like a textbox, but with chrome autofill off.

<input readonly onfocus="this.removeAttribute('readonly');" type="text">

将readonly属性添加到标记中,同时删除onfocus事件,可以修复该问题

你必须添加这个属性:

autocomplete="new-password"

来源链接:全文

我的工作,因为上面没有一个似乎工作在Chrome 63及以上

我在我的网站上通过替换违规的输入元素来解决这个问题

<p class="input" contenteditable="true">&nbsp;</p>

在提交之前使用jQuery填充隐藏字段。

但这确实是一个可怕的黑客,因为铬的一个糟糕的决定。