Detecting Browser Autofill

前端 未结 29 1416
天涯浪人
天涯浪人 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 06:47

    Just in case someone is looking for a solution (just as I was today), to listen to a browser autofill change, here's a custom jquery method that I've built, just to simplify the proccess when adding a change listener to an input:

        $.fn.allchange = function (callback) {
            var me = this;
            var last = "";
            var infunc = function () {
                var text = $(me).val();
                if (text != last) {
                    last = text;
                    callback();
                }
                setTimeout(infunc, 100);
            }
            setTimeout(infunc, 100);
        };
    

    You can call it like this:

    $("#myInput").allchange(function () {
        alert("change!");
    });
    

提交回复
热议问题