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


当前回答

我正在四处寻找解决方案,但没有一个在所有浏览器上都能正常工作。

我尝试了自动完成off、none、nope、new_input_64(甚至是一些有趣的文本),因为autocomplete属性希望传递字符串,无论是什么。

经过多次搜索和尝试,我找到了这个解决方案。

我将所有输入类型都改为文本,并添加了一些简单的CSS代码。

以下是表格:

<form class="row gy-3">
  <div class="col-md-12">
    <label for="fullname" class="form-label">Full Name</label>
    <input type="text" class="form-control" id="fullname" name="fullname" value="">
  </div>
  <div class="col-md-6">
    <label for="username" class="form-label">User Name</label>
    <input type="text" class="form-control" id="username" name="username" value="">
  </div>
  <div class="col-md-6">
    <label for="email" class="form-label">Email</label>
    <input type="text" class="form-control" id="email" name="email" value="">
  </div>
  <div class="col-md-6">
    <label for="password" class="form-label">Password</label>
    <input type="text" class="form-control pswd" id="password" name="password" value="">
  </div>
  <div class="col-md-6">
    <label for="password" class="form-label">Confirm Password</label>
    <input type="text" class="form-control pswd" id="password" name="password" value="">
  </div>
  <div class="col-md-12">
    <label for="recaptcha" class="form-label">ReCaptcha</label>
    <input type="text" class="form-control" id="recaptcha" name="recaptcha" value="">
  </div>
</form>

隐藏密码的CSS代码:

.pswd {
    -webkit-text-security: disc;
}

在Chrome、Opera和Edge上测试。

其他回答

这对我来说就像一种魅力。

将表单的自动完成属性设置为关闭添加虚拟输入字段,并将其属性设置为禁用。

<form autocomplete=“off”><input-type=“text”autocomplete=“off”style=“display:none”></form>

Chrome计划支持这一点。

目前,最好的建议是使用很少自动完成的输入类型。

Chrome讨论

<input type='search' name="whatever" />

要与Firefox兼容,请使用normalautocomplete='off'

<input type='search' name="whatever" autocomplete='off' />

最佳解决方案:

防止自动完成用户名(或电子邮件)和密码:

<input type="email" name="email"><!-- Can be type="text" -->
<input type="password" name="password" autocomplete="new-password">

阻止自动完成字段:

<input type="text" name="field" autocomplete="nope">

说明:autocomplete在<input>中继续工作,autocomplete=“off”不工作,但您可以将off更改为随机字符串,如nope。

工作地点:

铬:49、50、51、52、53、54、55、56、57、58、59、60、61、62、63和64Firefox:44、45、46、47、48、49、50、51、52、53、54、55、56、57和58

只需设置autocomplete=“off”。这样做有一个很好的理由:您希望提供自己的自动完成功能!

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