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

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

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

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

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


当前回答

除了autocomplete="off"外,还必须将值设置为空(value="")才能使其工作。

其他回答

如果你有问题保持占位符,但禁用chrome自动填充我发现这个解决办法。

问题

HTML

<div class="form">
    <input type="text" placeholder="name"><br>
    <input type="text" placeholder="email"><br>
    <input type="text" placeholder="street"><br>
</div>

http://jsfiddle.net/xmbvwfs6/1/

上面的例子仍然会产生自动填充的问题,但是如果你使用required="required"和一些CSS,你可以复制占位符,Chrome不会选择标签。

解决方案

HTML

<div class="form">
    <input type="text" required="required">
    <label>Name</label>  
    <br>
    <input type="text" required="required">
    <label>Email</label>    
    <br>
    <input type="text" required="required">
    <label>Street</label>    
    <br>
</div>

CSS

input {
    margin-bottom: 10px;
    width: 200px;
    height: 20px;
    padding: 0 10px;
    font-size: 14px;
}
input + label {
    position: relative;
    left: -216px;
    color: #999;
    font-size: 14px;
}
input:invalid + label { 
    display: inline-block; 
}
input:valid + label { 
    display: none; 
}

http://jsfiddle.net/mwshpx1o/1/

在Chromium 53.0.2785.92(64位)上,以下工作

<div style="visibility:hidden">
    <input type="text" name="fake_username_remembered">
    <input type="password" name="fake_password_remembered">
</div>

显示:none不工作

试试这个。我知道这个问题有点老了,但这是解决这个问题的另一种方法。

我还注意到这个问题出现在密码字段的上方。

两种方法我都试过了

<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="" />

希望它能帮助到一些人。

通过设置自动完成关闭应该在这里工作,我有一个例子,这是谷歌在搜索页面使用。我从inspect element找到了这个。

编辑: 如果off不起作用,则尝试false或nofill。在我的情况下,它是chrome 48.0版本

对于用户名密码组合,这是一个很容易解决的问题。Chrome启发式查找模式:

<input type="text">

紧随其后的是:

<input type="password">

简单地通过使其无效来中断这个过程:

<input type="text">
<input type="text" onfocus="this.type='password'">