Javascript Jquery 在输入中禁用自动完成功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14078067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery disable autocomplete in input
提问by Max Pain
I have this code:
我有这个代码:
The html:
html:
<input id="register_username" type="text">
<input id="register_password" type="text">
<input id="register_repeatpassword" type="text">
<input id="register_email" type="text">
The Jquery:
Jquery:
$(document).ready(function(){
$('#register_username').attr('autocomplete','off');
$('#register_password').attr('autocomplete','off');
$('#register_repeatpassword').attr('autocomplete','off');
$('#register_email').attr('autocomplete','off');
});
I want to disable the autocomplete feature offred by some browsers and acctually make the user type the entire field but the code is not working the dropdown menu still appear and the user still able to choose stuff the typed before. I also tried to user autocomplete="off"but nothing happened. What am I doing wrong here ?
我想禁用某些浏览器提供的自动完成功能,并实际让用户输入整个字段,但代码不起作用,下拉菜单仍然出现,用户仍然可以选择之前输入的内容。我也尝试过用户autocomplete="off"但什么也没发生。我在这里做错了什么?
回答by David Hellsing
Just add:
只需添加:
autocomplete="off"
as attributes to the INPUTelements, f.ex:
作为INPUT元素的属性,例如:
<input autocomplete="off" id="register_username" type="text" name="username">
回答by Carl Smith
The jQueryversion:
在jQuery的版本:
$("#register_username").attr("autocomplete", "off");
回答by rajesh kakawat
回答by Brad Christie
Though I agree, autocomplete="off"should be widely supported and is the end solution, it not working may be a result of the HTML5 spec on the autocomplete attribute:
尽管我同意,autocomplete="off"应该得到广泛支持并且是最终解决方案,但它不起作用可能是由于HTML5 规范对 autocomplete 属性的影响:
The autocompletion mechanism must be implemented by the user agent acting as if the user had modified the element's value, and must be done at a time where the element is mutable(e.g. just after the element has been inserted into the document, or when the user agent stops parsing).
自动完成机制必须由用户代理实现,就像用户修改了元素的值一样,并且必须在元素可变的时候完成(例如,在元素被插入到文档之后,或者当用户代理停止解析)。
That, to me, implies it should be an 'original' attribute of the element, and cannot be added post-render. And since most browsers are implementing the new spec1, chances are if they allowed it with javascript before they've probably corrected the implementation based on the above spec.
对我来说,这意味着它应该是元素的“原始”属性,并且不能在渲染后添加。并且由于大多数浏览器都在实现新的规范1,因此在他们可能根据上述规范更正实现之前,他们是否允许使用 javascript 的可能性很大。
If you haven't already, try adding the attribute directly to the controls instead of relying on jQuery doing so on [presumably] document ready.
如果您还没有,请尝试将属性直接添加到控件,而不是依赖 jQuery 在 [大概] 文档就绪时这样做。
1I use spec losely here since HTML5 isn't a standard, but a lot of browsers are implementing some of the more concrete features. Since autocompletehas been around since IE5, it's probably one of those ones you can consider a safe implementation.
1由于 HTML5 不是标准,因此我在这里使用了规范,但许多浏览器正在实现一些更具体的功能。由于autocomplete自 IE5以来一直存在,它可能是您可以考虑安全实现的那些之一。
回答by Jared
in case someone comes here looking for how to turn off their jquery autocomplete:
如果有人来这里寻找如何关闭他们的 jquery 自动完成功能:
$("#elementId").autocomplete({
disabled: true
});
回答by Cava
Now a days you need change "off" to another random string like "nope":
现在,您需要将“关闭”更改为另一个随机字符串,例如“nope”:
Jquery:
查询:
$("#register_username").attr("autocomplete", "nope");
HTML:
HTML:
<input id="register_username" type="text" autocomplete="nope">
回答by Wadih M.
Working as of April 23rd 2019.
截至 2019 年 4 月 23 日工作。
If you want to disable Chrome's autofill on everything on your page, especially if you don't use forms but rather serialized jquery selectors to submit your data.
如果您想在页面上的所有内容上禁用 Chrome 的自动填充,尤其是当您不使用表单而是使用序列化的 jquery 选择器来提交数据时。
Doing it at runtime had the best results for me, and Chrome will autofill on runtime anyways, so it's a justifiable solution to counter Chrome's behavior.
在运行时执行它对我来说效果最好,而且 Chrome 无论如何都会在运行时自动填充,所以这是对抗 Chrome 行为的合理解决方案。
Simply add this at the bottom of your code and Chrome won't try to force auto-fill things:
只需在代码底部添加此内容,Chrome 就不会尝试强制自动填充内容:
<script>
$(document).ready(function () {
$("input").attr("autocomplete", "new-password");
});
</script>
回答by Prakash
<form id='myForm'>
<input id="register_username" type="text">
<input id="register_password" type="text">
<input id="register_repeatpassword" type="text">
<input id="register_email" type="text">
</form>
$(document).ready(function(){
$('#myForm').on( 'focus', ':input', function(){
$(this).attr( 'autocomplete', 'off' );
});
});
回答by Francis Ducharme
Using jQuery, I do this in a layout page, or a bit of HTML that will be present on every page of the website.
使用 jQuery,我在布局页面中执行此操作,或者在网站的每个页面上都显示一些 HTML。
$(document).ready(function () {
//when the page is done loading, disable autocomplete on all inputs[text]
$('input[type="text"]').attr('autocomplete', 'off');
//do the same when a bootstrap modal dialog is opened
$(window).on('shown.bs.modal', function () {
$('input[type="text"]').attr('autocomplete', 'off');
});
});

