如何在主要浏览器中禁用特定输入(或表单字段)的自动完成?
当前回答
大多数答案都没有帮助,因为浏览器只是忽略了它们。(其中一些不兼容跨浏览器)。对我有效的修复方法是:
<form autocomplete="off">
<input type="text" autocomplete="new-password" />
<input type="password" autocomplete="new-password" />
</form>
我在表单标签上设置autofill=“off”,在不需要自动填充的地方设置autofil=“new password”。
其他回答
只需设置autocomplete=“off”。这样做有一个很好的理由:您希望提供自己的自动完成功能!
我正在四处寻找解决方案,但没有一个在所有浏览器上都能正常工作。
我尝试了自动完成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上测试。
最简单的答案是
<input autocomplete=“on | off”>
但请记住浏览器支持。当前,支持自动完成属性
铬17.0及最新版本IE 5.0及最新版本Firefox 4.0及最新版本Safari 5.2及最新版本Opera 9.6及最新版本
我的jQuery解决方案。它可能不是100%可靠,但它对我有用。这个想法在代码注释中描述。
/***防止字段自动填充字段。*当关注具有自动完成功能的文本字段(值为:“off”、“none”、“false”)时,我们将该值替换为新的唯一值(此处为-“off forced-[TIMESTAMP]”),*浏览器在保存的值中找不到这种类型的自动完成,并且不提供选项。*然后,为了防止输入的文本保存在浏览器中,以便进行新的唯一自动完成,当字段失去焦点或用户按下Enter键时,我们将其替换为先前设置的文本。*@type{{init:*}}*/var PreventFieldsAutofill=(函数(){函数init(){events.onPageStart();}var事件={onPageStart:函数(){$(document).on('fous','输入[autocomplete=“off”],输入[autocomplete=“none”],input[autocomplete=“false”]',函数(){methods.replaceAttrs($(this));});$(document).on('blur','input[data prev autocomplete]',函数(){methods.returnAttrs($(this));});$(document).on('keydown','input[data prev autocomplete]',函数(事件){if(event.keyCode==13 | | event.which==13){methods.returnAttrs($(this));}});$(document).on('submit','form',函数(){$(this).find('input[data prev autocomplete]').each(函数(){methods.returnAttrs($(this));});});}};var方法={/***将autocomplete和name属性的值替换为unique,并将原始值保存为新的数据属性*@param$输入*/replaceAttrs:函数($input){var randomString='off-forced-'+Date.now();$input.attr('data-prev-autocomplete',$input.aattr('自动完成'));$input.attr('自动完成',随机字符串);if($input.attr('name')){$input.attr('data-prev-name',$input.atr('name'));$input.attr('name',randomString);}},/***恢复原始自动完成和名称值,以防止在浏览器中保存文本以获取唯一值*@param$输入*/returnAttrs:函数($input){$input.attr('自动完成',$input.atr('数据-版本-自动完成'));$input.removeAttr('data-prev-autocomplete');if($input.attr('data-prev-name')){$input.attr('name',$input.atr('data-prev-name'));$input.removeAttr('data-prev-name');}}};返回{init:初始化}})();PreventFieldsAutofill.init();.输入{显示:块;宽度:90%;填充:6px 12px;字体大小:14px;线高:1.42857143;颜色:#555555;背景色:#fff;背景图像:无;边框:1px实心#ccc;边界半径:4px;}<script src=“https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js“></script><form action=“#”><p><label for=“input-1”>Firts name without autocomplete</label><br/><input id=“input-1”class=“input”type=“text”name=“first name”autocomplete=“off”placeholder=“Firts name without autocomplete”/></p><p><label for=“input-2”>Firts name with autocomplete</label><br/><input id=“input-2”class=“input”type=“text”name=“first name”autocomplete=“given name”placeholder=“Firts name with autocomplete”/></p><p><button type=“submit”>提交表单</button></p></form>
如果您在JavaScript中动态设置autocomplete=“off”,Safari不会改变对自动完成的看法。然而,如果您在每个字段的基础上执行此操作,则会受到尊重。
$(':input', $formElement).attr('autocomplete', 'off');
推荐文章
- 使伸缩项目正确浮动
- 形式内联内的形式水平在twitter bootstrap?
- 自定义元素在HTML5中有效吗?
- 如何触发自动填充在谷歌Chrome?
- 创建圈div比使用图像更容易的方法?
- 为什么Chrome浏览器不正确地确定页面是在不同的语言,并提供翻译?
- 在网页上用鼠标模拟震颤(例如帕金森病)?
- Bootstrap抛出Uncaught错误:Bootstrap的JavaScript需要jQuery
- 如何改变文本区域的边框颜色:焦点
- 我如何设置背景颜色为文本的宽度,而不是整个元素的宽度,使用CSS?
- 如何删除和清除所有的本地存储数据
- 强制打开“另存为…”弹出打开文本链接点击PDF在HTML
- 如何修改标签文本?
- 在HTML中还有其他有用的空格码吗,比如半空格的 , em-spaces, en-spaces等等?
- 输入触发器按钮单击