如何在主要浏览器中禁用特定输入(或表单字段)的自动完成?


当前回答

我必须花了两天的时间来讨论这一问题,然后才能制定自己的解决方案:

首先,如果它适用于任务,则放弃输入并使用文本区域。据我所知,自动填充/自动完成在文本区域没有业务,就我们正在努力实现的目标而言,这是一个很好的开始。现在,您只需更改该元素的一些默认行为,使其充当输入。

接下来,您将希望将任何长条目保持在同一行上,如输入,并希望文本区域与光标一起沿y轴滚动。我们还想去掉调整大小框,因为我们正在尽力模拟输入的行为,而输入没有调整大小句柄。我们通过一些快速CSS实现了所有这些:

#您的文本区域{调整大小:无;overflow-y:隐藏;空白:nowrap;}

最后,您需要确保讨厌的滚动条不会出现,破坏聚会(对于那些特别长的条目),因此确保文本区域不会显示:

#您的文本区域::-webkit滚动条{显示:无;}

轻松点。此解决方案没有自动完成问题。

其他回答

我的解决方案是使用angular js指令动态更改文本输入类型它就像魅力一样

首先添加2个隐藏文本字段

然后添加一个像这样的角度指令

 (function () {

    'use strict';

    appname.directive('changePasswordType', directive);

    directive.$inject = ['$timeout', '$rootScope',  '$cookies'];

    function directive($timeout,  $rootScope, $cookies) {
        var directive = {
            link: link,
            restrict: 'A'
        };

        return directive;

        function link(scope,element) {
            var process = function () {
                var elem =element[0];
                elem.value.length > 0 ? element[0].setAttribute("type", "password") :
                element[0].setAttribute("type", "text");
            }

            element.bind('input', function () {
                process();
            });

            element.bind('keyup', function () {
                process();
            });
        }
    }
})()

然后在需要阻止自动完成的文本字段中使用它

    <input type="text" style="display:none">\\can avoid this 2 lines
    <input type="password" style="display:none">
    <input type="text"  autocomplete="new-password" change-password-type>

注意:不要忘记包含jquery,并在开始时设置type=“text”

为了解决这个问题,我使用了一些CSS技巧和以下工作。

input {
    text-security:disc;
    -webkit-text-security:disc;
    -mox-text-security:disc;
}

请阅读本文了解更多详情。

您可以在输入控件中使用autocomplete=off来避免自动完成

例如:

<input type=text name="test" autocomplete="off" />

如果上面的代码不起作用,那么也尝试添加这些属性

autocapitalize="off" autocomplete="off"

or

将输入类型属性更改为type=“search”。谷歌不会对搜索类型的输入应用自动填充。

提供的答案在我测试的所有浏览器上都不起作用。基于已经提供的答案,这是我最终在Chrome 61、Microsoft Edge 40(EdgeHTML 15)、IE 11、Firefox 57、Opera 49和Safari 5.1上测试的结果。由于多次试验,它很古怪;然而,它确实对我有用。

<form autocomplete="off">
    ...
    <input type="password" readonly autocomplete="off" id="Password" name="Password" onblur="this.setAttribute('readonly');" onfocus="this.removeAttribute('readonly');" onfocusin="this.removeAttribute('readonly');" onfocusout="this.setAttribute('readonly');" />
    ...
</form> 

<script type="text/javascript">
    $(function () {           
        $('input#Password').val('');
        $('input#Password').on('focus', function () {
        if (!$(this).val() || $(this).val().length < 2) {
            $(this).attr('type', 'text');
        }
        else {
            $(this).attr('type', 'password');
        }
    });
    $('input#Password').on('keyup', function () {
        if (!$(this).val() || $(this).val().length < 2) {
            $(this).attr('type', 'text');
        }
        else {
            $(this).attr('type', 'password');
        }
    });
    $('input#Password').on('keydown', function () {
        if (!$(this).val() || $(this).val().length < 2) {
            $(this).attr('type', 'text');
        }
        else {
            $(this).attr('type', 'password');
        }
    });
</script>

没有虚假输入,没有javascript!

无法在浏览器之间一致禁用自动填充。我尝试了所有不同的建议,但没有一个在所有浏览器中都有效。唯一的方法是根本不使用密码输入。下面是我想到的:

<style type="text/css">
    @font-face {
        font-family: 'PasswordDots';
        src: url('text-security-disc.woff') format('woff');
        font-weight: normal;
        font-style: normal;
    }

    input.password {
        font-family: 'PasswordDots' !important;
        font-size: 8px !important;
    }
</style>

<input class="password" type="text" spellcheck="false" />

下载:text-security-disc.woff

以下是我的最终结果:

负面的副作用是,可以从输入中复制纯文本,但使用一些JS应该可以防止这种情况。