Detecting Browser Autofill

前端 未结 29 1929
天涯浪人
天涯浪人 2020-11-22 06:15

How do you tell if a browser has auto filled a text-box? Especially with username & password boxes that autofill around page load.

My first question is when does

29条回答
  •  暖寄归人
    2020-11-22 07:03

    My solution is:

        $.fn.onAutoFillEvent = function (callback) {
            var el = $(this),
                lastText = "",
                maxCheckCount = 10,
                checkCount = 0;
    
            (function infunc() {
                var text = el.val();
    
                if (text != lastText) {
                    lastText = text;
                    callback(el);
                }
                if (checkCount > maxCheckCount) {
                    return false;
                }
                checkCount++;
                setTimeout(infunc, 100);
            }());
        };
    
      $(".group > input").each(function (i, element) {
          var el = $(element);
    
          el.onAutoFillEvent(
              function () {
                  el.addClass('used');
              }
          );
      });
    

提交回复
热议问题