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

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

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

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

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


当前回答

我不知道为什么,但这对我很有帮助。

<input type="password" name="pwd" autocomplete="new-password">

我不知道为什么,但autocomplete="new-password"禁用自动填充。它在最新的49.0.2623.112 chrome版本中工作。

其他回答

这里有一个肮脏的hack -

你的元素在这里(添加了disabled属性):

<input type="text" name="test" id="test" disabled="disabled" />

然后在你的网页底部放一些JavaScript:

<script>
    setTimeout(function(){
        document.getElementById('test').removeAttribute("disabled");
        },100);
</script>

通过这个技巧,自动完成功能已经成功禁用。 它的工作原理!

[HTML]

<div id="login_screen" style="min-height: 45px;">
   <input id="password_1" type="text" name="password">
</div>

(JQuery)

$("#login_screen").on('keyup keydown mousedown', '#password_1', function (e) {
    let elem = $(this);

    if (elem.val().length > 0 && elem.attr("type") === "text") {
        elem.attr("type", "password");
    } else {
        setTimeout(function () {
            if (elem.val().length === 0) {
                elem.attr("type", "text");
                elem.hide();
                setTimeout(function () {
                    elem.show().focus();
                }, 1);
            }
        }, 1);
    }

    if (elem.val() === "" && e.type === "mousedown") {
        elem.hide();
        setTimeout(function () {
            elem.show().focus();
        }, 1);
    }

});

最新的解决方案是添加autocomplete="new-password"到密码字段,以防止Chrome自动填充它。

然而,正如sibbl所指出的,这并不会阻止Chrome在表单提交完成后要求你保存密码。在Chrome 51.0.2704.106中,我发现你可以通过添加一个不可见的虚拟密码字段来实现这一点,该字段还具有属性autocomplete="new-password"。注意,“display:none”在这种情况下不起作用。在真正的密码字段之前添加如下内容:

<input type="password" autocomplete="new-password" 
style="visibility:hidden;height:0;width:1px;position:absolute;left:0;top:0">`

这只适用于当我将宽度设置为非零值时。感谢tibalt和fareed namrouti给出的原始答案。

不幸的是,这些解决方案似乎都不起作用。我能够空白电子邮件(用户名)使用

                    <!-- fake fields are a workaround for chrome autofill getting the wrong fields -->
                    <input style="display:none" type="text" name="fakeusernameremembered"/>
                    <input style="display:none" type="password" name="fakepasswordremembered"/>

技术,但密码仍然流行。

这是我最近使用的解决方案。

$('#email').prop('autocomplete', true);