我遇到了chrome自动填充行为的几个形式的问题。
表单中的字段都有非常常见和准确的名称,例如“email”、“name”或“password”,并且它们还设置了autocomplete=“off”。
自动完成标志已经成功禁用了自动完成行为,当你开始输入时,会出现一个下拉的值,但没有改变Chrome自动填充字段的值。
这种行为是可以的,除了chrome填充输入不正确,例如填充电话输入与电子邮件地址。客户抱怨过这个问题,所以它被证实在很多情况下都发生了,而不是我在我的机器上本地操作的某种结果。
目前我能想到的唯一解决方案是动态生成自定义输入名称,然后在后端提取值,但这似乎是一种相当笨拙的解决这个问题的方法。是否有任何标签或怪癖,改变自动填充行为,可以用来解决这个问题?
2016年谷歌Chrome开始忽略autocomplete=off,尽管它在W3C中。他们发布的答案是:
这里棘手的部分是,在web的某个地方,autocomplete=off成为许多表单字段的默认值,而没有真正考虑这是否对用户有好处。这并不意味着不存在不希望浏览器自动填充数据的有效情况(例如在CRM系统上),但总的来说,我们认为这些是少数情况。因此,我们开始忽略autocomplete=off的Chrome自动填充数据。
本质上说:我们更了解用户想要什么。
当需要autocomplete=off时,他们打开了另一个错误来发布有效的用例
在我所有的B2B应用程序中,我还没有看到与自动完成相关的问题,但只在输入密码类型时出现。
如果屏幕上有任何密码字段,即使是隐藏的,自动填充步骤。
要打破这种逻辑,如果不打破自己的页面逻辑,可以将每个密码字段放入自己的表单中。
<input type=name >
<form>
<input type=password >
</form>
试试这个。我知道这个问题有点老了,但这是解决这个问题的另一种方法。
我还注意到这个问题出现在密码字段的上方。
两种方法我都试过了
<form autocomplete="off">和<input autocomplete="off">,但它们都不适合我。
所以我使用下面的代码片段修复了它-只是在密码类型字段上方添加了另一个文本字段,并使其显示为:none。
就像这样:
<input type="text" name="prevent_autofill" id="prevent_autofill" value="" style="display:none;" />
<input type="password" name="password_fake" id="password_fake" value="" style="display:none;" />
<input type="password" name="password" id="password" value="" />
希望它能帮助到一些人。
所以显然,最好的修复/黑客现在不再工作,再次。我使用的Chrome版本是49.0.2623.110 m和我的帐户创建形式现在显示保存的用户名和密码与形式无关。由于铬!其他黑客似乎很可怕,但这个黑客不那么可怕……
为什么我们需要这些黑客工作是因为这些形式通常是帐户创建形式,即不是登录形式,应该允许填写密码。帐户创建表单您不希望删除用户名和密码的麻烦。从逻辑上讲,这意味着在呈现时永远不会填充密码字段。所以我使用了一个文本框,加上一点javascript。
<input type="text" id="password" name="password" />
<script>
setTimeout(function() {
$("#password").prop("type", "password");
}, 100);
// time out required to make sure it is not set as a password field before Google fills it in. You may need to adjust this timeout depending on your page load times.
</script>
我认为这是可以接受的,因为用户不会在短时间内获得密码字段,并且如果该字段是密码字段,则发送回服务器没有任何区别,因为无论如何它都是以纯文本发送回来的。
Caveat:
If, like me, you use the same creation form as an update form things might get tricky. I use mvc.asp c# and when I use @Html.PasswordFor() the password is not added to the input box. This is a good thing. I have coded around this. But using @Html.TextBoxFor() and the password will be added to the input box, and then hidden as a password. However as my passwords are hashed up, the password in the input box is the hashed up password and should never be posted back to the server - accidentally saving a hashed up hashed password would be a pain for someone trying to log in. Basically... remember to set the password to an empty string before the input box is rendered if using this method.
经过几个月的努力,我发现解决办法比你想象的要简单得多:
用autocomplete="false"代替autocomplete="off";)
就这么简单,它的工作就像一个魅力在谷歌Chrome浏览器以及!
2019年8月更新(评论中来自@JonEdiger)
注意:网上有很多信息说浏览器现在处理autocomplete='false'与autocomplete='off'相同。至少在这一刻,它阻止了这三个浏览器的自动完成。
在表单级别设置它,然后对于你想关闭的输入,设置为一些无效的值,比如'none':
<form autocomplete="off">
<input type="text" id="lastName" autocomplete="none"/>
<input type="text" id="firstName" autocomplete="none"/>
</form>