简体   繁体   English

如果浏览器自动填充输入字段,则调用函数

[英]Call a function if browser autofills an input field

I'm trying to invoke a function that detects whether the browser has autofilled a field, and if so, add a class to it. 我正在尝试调用一个函数,该函数检测浏览器是否已自动填充字段,如果是,则向其中添加一个类。

I found a thread with a solution that works partially: https://stackoverflow.com/a/25393087 我发现了一个带有部分解决方案的线程: https : //stackoverflow.com/a/25393087

Here's my integration of it: 这是我的整合:

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

$(".input").allchange(function () {
    $('.input').addClass('not--empty');
});

The problem I'm having is that I also have a function that checks for a change to the input on blur and also adds a class to that input. 我遇到的问题是我还有一个函数可以检查模糊时输入的变化,并向该输入添加一个类。 However, the above function is causing it to be added to all instances of '.input'. 但是,以上函数导致将其添加到“ .input”的所有实例中。

I think the solution would be to invoke the function on only the autofilled element, but when I run the function below, it doesn't work: 我认为解决方案是仅在自动填充的元素上调用该函数,但是当我在下面运行该函数时,它不起作用:

$(".input").allchange(function () {
    $(this).addClass('not--empty');
});

Any ideas how to get this working? 任何想法如何使它工作?

EDIT: Thought I'd add an image to clarify what I'm referring to: 编辑:以为我会添加一个图像来澄清我指的是:

在此处输入图片说明

So this actually turned out to be a very simple solution – and it may not work for all browsers, but it works flawlessly for Chrome/Firefox/Safari latest, which for this application is fine. 因此,这实际上是一个非常简单的解决方案-可能不适用于所有浏览器,但对于最新的Chrome / Firefox / Safari而言,它可以完美地工作,对于该应用程序来说,这是很好的选择。

setTimeout(function() {
    $('.input:-webkit-autofill').each(function() {
        var elem = $(this);
        $(elem).addClass('not--empty');
    })
},100);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检测用户何时开始在密码字段中输入文本,而不被浏览器自动填充/自动完成触发 - How to detect when user first starts typing text into password field, without being triggered by browser autofills/autocompletes Google Chrome问题:输入类型“密码”的模型未更新,但“文本”在浏览器自动填充保存的值时执行 - Google Chrome issue: Model for input type 'password' not updated but 'text' does when browser autofills saved values 使用浏览器控制台在输入字段上单击()函数 - click() function on input field using browser console 在输入搜索字段中输入时调用函数 - call function on hit enter in input search field 离开输入字段后调用函数 - Call a function after leaving input field 在输入字段中的任何更改上调用jQuery函数 - Call jQuery function on any change in input field 来自文本字段的输入会根据数据自动填充其他文本字段 - Input from a textfield autofills the other textfields based on data 如何告诉浏览器使用JQuery清空字段,然后调用该函数? - How can I tell the browser to empty a field by using JQuery and then call the function? 停止内部锚标记函数调用外部输入字段函数javascript - Stop inner anchor tag function call outer input field function javascript 基于输入的调用功能 - Call function based on input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM