浏览器自动填充和 Javascript 触发事件

发布于 2024-10-16 08:43:22 字数 98 浏览 3 评论 0原文

我们的一位用户刚刚提出了这样一个事实:他们的浏览器自动填充不会导致 JS onChange 事件触发;这会导致我们的用户注册出现问题。

这是设计使然吗?有办法解决吗?

One of our users just brought up the fact that their browsers Autofill doesn't cause JS onChange events to fire; this causes a problem with user registration for us.

Is this by design? Is there a way to work around it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

刘备忘录 2024-10-23 08:43:22

我偶尔使用的一种解决方案是检查字段/输入/选择的值是否与其默认值不同。 defaultValue 是标记中最初的值,value 是当前值,也称为选择或输入的值。即使表单是自动填充的,这也可能会有所不同。

如果您想完全关闭自动填充,添加 autocomplete="off" 可能是明智之举
与您的逻辑直接相关的字段。

One solution I have been using occasionally is to check whether the value of the field/input/select differs from it's defaultValue. defaultValue would be the value that was originally in the markup, and value is the current value aka selected or entered value. This would probably differ even though the form was autopopulated.

If you want to turn off autofill altogether, it might be wise to add autocomplete="off"
on fields that are directly connected to your logic.

夜唯美灬不弃 2024-10-23 08:43:22

如果您想获得自动填充行为但更改样式,也许您可​​以执行以下操作(jQuery):

$(window).load(function(){  
  if($('input:-webkit-autofill')){   
    $('input:-webkit-autofill').each(function(){
      //put your conditions here 
    });   
    // RE-INITIALIZE VARIABLES HERE IF YOU SET JQUERY OBJECT'S TO VAR FOR FASTER PROCESSING 
}});

If you want to get the autofill behaviour but change the styling, maybe you can do something like this (jQuery):

$(window).load(function(){  
  if($('input:-webkit-autofill')){   
    $('input:-webkit-autofill').each(function(){
      //put your conditions here 
    });   
    // RE-INITIALIZE VARIABLES HERE IF YOU SET JQUERY OBJECT'S TO VAR FOR FASTER PROCESSING 
}});
草莓味的萝莉 2024-10-23 08:43:22

您是否尝试过使用 onpropertychanged 而不是 onchange 事件?但这仅适用于 IE,并且是 MSDN 上推荐的修复方法。

Have you tried using the onpropertychanged instead of onchange event? That's for IE only though and is the recommended fix on MSDN.

迎风吟唱 2024-10-23 08:43:22

以防万一有人仍在寻找解决方案(就像我今天一样),监听浏览器自动填充更改,这是我构建的自定义 jquery 方法,只是为了简化向输入添加更改侦听器时的过程:

    $.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);
    };

你可以这样称呼它:

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

Just in case someone is still 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!");
});
戏蝶舞 2024-10-23 08:43:22

这是一个非常好的解决方案,其功能与 jishi 所描述的类似:

https://web.archive.org/web/20131125153914/http://furrybrains.com/2009/01/02/capturing-autofill-as- a-change-event/

使用 Wayback Machine 链接更新了损坏的链接

Here's a pretty good solution that does something similar to what jishi described:

https://web.archive.org/web/20131125153914/http://furrybrains.com/2009/01/02/capturing-autofill-as-a-change-event/

Updated the broken link with Wayback Machine link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文