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


当前回答

许多现代浏览器不再支持登录字段的autocomplete=“off”。autocomplete=“新密码”是wokring,更多信息MDN文档

其他回答

我一直在尝试无尽的解决方案,然后我发现:

不用autocomplete=“off”,只需使用autocomplete==“false”

就这么简单,它在谷歌Chrome中也很有魅力!

Chrome一直试图强制自动完成。所以你需要做一些事情。

输入字段的属性id和名称不需要是可识别的。所以没有姓名、电话、地址等值。

相邻的标签或div也需要不可识别。然而,您可能会想,用户如何知道?好吧,在使用内容后,您可以使用::做一个有趣的小技巧。你可以将其设置为字母。

<label>Ph<span class='o'></span>ne</label>
<input id='phnbr' name='phnbr' type='text'>

<style>
  span.o {
    content: 'o';
  }
</style>

用只读存储器贴出的答案很聪明,很管用。

但当我使用Bootstrap时,只读输入字段被标记为灰色背景,直到被聚焦。当文档加载时,您可以通过简单地锁定和解锁输入来欺骗浏览器。

因此,我有一个想法将其实现到jQuery解决方案中:

jQuery(document).ready(function () {
    $("input").attr('readonly', true);
    $("input").removeAttr('readonly');
});

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

三个选项:

第一:

<input type='text' autocomplete='off' />

第二:

<form action='' autocomplete='off'>

第三个(JavaScript代码):

$('input').attr('autocomplete', 'off');