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


当前回答

Google Chrome忽略某些输入的autocomplete=“off”属性,包括密码输入和按名称检测的常见输入。

例如,如果您输入的是姓名地址,那么Chrome将根据其他网站上输入的地址提供自动填充建议,即使您告诉它不要这样做:

<input type="string" name="address" autocomplete="off">

如果您不希望Chrome这样做,那么可以重命名或命名表单字段的名称:

<input type="string" name="mysite_addr" autocomplete="off">

如果您不介意以前在站点上输入的自动完成值,那么可以保持启用自动完成。域名的名称间距应足以防止从其他站点记住的值出现。

<input type="string" name="mysite_addr" autocomplete="on">

其他回答

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

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

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

解决方法是在用户想要更改密码之前,不要将密码字段插入到DOM中。这可能适用于某些情况:

在我们的系统中,管理员页面中有一个密码字段,因此我们必须避免无意中设置其他用户的密码。由于这个原因,表单有一个额外的复选框,用于切换密码字段的可见性。

因此,在这种情况下,来自密码管理器的自动填充成为双重问题,因为用户甚至看不到输入。

解决方案是让复选框触发是否在DOM中插入密码字段,而不仅仅是其可见性。

AngularJS的伪实现:

<input type="checkbox" ng-model="createPassword">
<input ng-if="changePassword" type="password">

如果仅自动完成=“off”不起作用,也可以尝试以下操作:

autocorrect="off" autocapitalize="off" autocomplete="off"

这就是:

函数turnOnPasswordStyle(){$('#inputpassword').attr('type',“password”);}<input-onput=“turnOnPasswordStyle()”id=“inputpassword”type=“text”>

提供的答案在我测试的所有浏览器上都不起作用。基于已经提供的答案,这是我最终在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>