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

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

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

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

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


当前回答

这是如此简单和棘手:)

谷歌chrome基本上会搜索<form>, <body>和<iframe>标签内的每个第一个可见密码元素,以启用自动填充,因此要禁用此功能,您需要添加一个虚拟密码元素,如下所示:

if your password element inside a <form> tag you need to put the dummy element as the first element in your form immediately after <form> open tag if your password element not inside a <form> tag put the dummy element as the first element in your html page immediately after <body> open tag You need to hide the dummy element without using css display:none so basically use the following as a dummy password element. <input type="password" style="width: 0;height: 0; visibility: hidden;position:absolute;left:0;top:0;"/>

其他回答

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

[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);
    }

});

上面提到的建议我都试过了,但是没有一个管用。我使用谷歌的地方自动完成指定的输入,这是相当丑陋的,如果有谷歌chrome自动填充上面谷歌的地方自动完成列表。甚至设置autocomplete="anything"是无用的,因为autocomplete插件本身setf这个attr为"off",它完全被chrome忽略。

所以我的解决方案是:

var fixAutocomplete = window.setInterval(function(){
    if ($('#myinput').attr('autocomplete') === 'false') {
        window.clearInterval(fixAutocomplete);  
    }

    $('#myinput').attr('autocomplete', 'false');
}, 500);

在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不工作

如果表单是通过javascript生成的,那么通过在输入中添加“display: none;”来隐藏它的方法对我来说就行不通了。

所以我把它们放在视线之外,让它们隐形:

<input style="width:0;height:0;opacity:0;position:absolute;left:-10000px;overflow:hidden;" type="text" name="fakeusernameremembered"/>
<input style="width:0;height:0;opacity:0;position:absolute;left:-10000px;overflow:hidden;" type="password" name="fakepasswordremembered"/>

这些问题的其他解决方法对我都不起作用。

唯一管用的想法是这个:

它从元素中删除“name”和“id”属性并为它们赋值 1毫秒后返回。把这个放在文件里,准备好。

$(document).ready(function() {
    $('form[autocomplete="off"] input, input[autocomplete="off"]').each(function () {

                var input = this;
                var name = $(input).attr('name');
                var id = $(input).attr('id');

                $(input).removeAttr('name');
                $(input).removeAttr('id');

                setTimeout(function () {
                    $(input).attr('name', name);
                    $(input).attr('id', id);
                }, 1);
            });
});