jquery - 关闭所有表单的“自动完成”(即使是尚未加载的表单)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2860712/
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 - turning "autocomplete" to off for all forms (even ones not loaded yet)
提问by Matthew
So, I've got this code:
所以,我有这个代码:
$(document).ready(function(){
$('form').attr('autocomplete', 'off');
});
It works great for all forms already existing. The problem is, some forms of mine are in pop ups loaded throught ajax. This won't apply to them since they're "loaded" later.
它适用于所有已经存在的形式。问题是,我的某些形式在通过 ajax 加载的弹出窗口中。这不适用于它们,因为它们稍后“加载”。
I know there's a live() function - but that's only for attaching events. What's a good way to apply this to allforms?
我知道有一个 live() 函数 - 但这仅用于附加事件。将此应用于所有表单的好方法是什么?
Thanks.
谢谢。
回答by PetersenDidIt
You could bind a live event to the focus event and then change the attribute on that.
您可以将实时事件绑定到焦点事件,然后更改其属性。
$(document).ready(function(){
$(':input').live('focus',function(){
$(this).attr('autocomplete', 'off');
});
});
As of jQuery 1.7 jQuery.livewas deprecated. This code would now look like:
从 jQuery 1.7开始,不推荐使用jQuery.live。这段代码现在看起来像:
$(document).ready(function(){
$( document ).on( 'focus', ':input', function(){
$( this ).attr( 'autocomplete', 'off' );
});
});
回答by Andy Brudtkuhl
The live event is now deprecated, you should us the on event instead. This will attach to every input in the DOM no matter if it's created yet or not. It will then add the autocomplete="off" attribute to that input.
现在不推荐使用实时事件,您应该改为使用 on 事件。这将附加到 DOM 中的每个输入,无论它是否已创建。然后它会将 autocomplete="off" 属性添加到该输入。
$(document).ready(function() {
$(document).on('focus', ':input', function() {
$(this).attr('autocomplete', 'off');
});
});
回答by elin3t
use jquery could don't be enough, because some browsers like chrome could load the 'autocomplete' saved data before document is ready ( $(document).ready)
使用 jquery 可能还不够,因为某些浏览器(例如 chrome)可以在文档准备好之前加载“自动完成”保存的数据($(document).ready)
jquery works like this
jquery 是这样工作的
$('input, :input').attr('autocomplete', 'off');
but add manually autocomplete='off'
to your input tags could be more efficient
但手动添加autocomplete='off'
到您的输入标签可能会更有效
回答by Prashant Belkhede
This works fine for me in Chrome as well.
这对我来说在 Chrome 中也很好用。
$("#autocmpldisablechk").click(function () {
if (!$(this).is(":checked")) {
// enable autocomplete
$("#autocomplete").autocomplete({
disabled: false
});
}
else {
// disable autocomplete
$("#autocomplete").autocomplete({
disabled: true
});
}
});