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

前端 未结 4 1925
时光说笑
时光说笑 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:06

    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');
      });
    });
    
    0 讨论(0)
  • 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' );
        });
    });
    
    0 讨论(0)
  • 2020-12-16 13:16

    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

    0 讨论(0)
  • 2020-12-16 13:26

    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 讨论(0)
提交回复
热议问题