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

前端 未结 4 1929
时光说笑
时光说笑 2020-12-16 12:31

So, I\'ve got this code:

$(document).ready(function(){
    $(\'form\').attr(\'autocomplete\', \'off\');
});

It works great for all forms al

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-16 13:09

    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' );
        });
    });
    

提交回复
热议问题