开发者

jquery - turning "autocomplete" to off for all forms (even ones not loaded yet)

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.

I know there's a live() function - 开发者_开发知识库but that's only for attaching events. What's a good way to apply this to all forms?

Thanks.


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.live was deprecated. This code would now look like:

$(document).ready(function(){
    $( document ).on( 'focus', ':input', function(){
        $( this ).attr( 'autocomplete', 'off' );
    });
});


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.

$(document).ready(function() {
  $(document).on('focus', ':input', function() {
    $(this).attr('autocomplete', 'off');
  });
});


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 works like this

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

but add manually autocomplete='off' to your input tags could be more efficient


This works fine for me in Chrome as well.

$("#autocmpldisablechk").click(function () {
    if (!$(this).is(":checked")) { 
        // enable autocomplete
        $("#autocomplete").autocomplete({
            disabled: false
        });
    }
    else { 
        // disable autocomplete
        $("#autocomplete").autocomplete({
            disabled: true
        });
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜