我想触发一个事件,无论焦点在HTML文本输入框,每一次数据的变化我想、输入框、文本、事件

2023-09-10 13:39:50 作者:时光瘦了

您如何抓住客户端事件时,浏览器中使用浏览器的自动完成功能对于一个普通的HTML输入字段改变输入文本字段? (这是由浏览器提供的输入字段中的小下拉菜单中,我知道,你可以在每个输入领域的一些浏览器与自动完成=关闭禁用它。)

How do you catch the client-side event when a browser changes an input text field using the browser's autocomplete feature for a plain HTML input field? (This is the small dropdown on input fields that is supplied by the browser, I know that you can disable it with "autocomplete=off" in some browsers per input field.)

我已经绑定到更改/ KEYUP事件,但是当用户开始输入一个值,并使用浏览器的原生自动完成,以填补了该领域的其他部分,这些不处理的情况下(和保持在该领域的焦点。)

I'm already binding to the "change/keyup" event, but these don't handle the case when the user starts typing in a value and uses the Browser's native autocomplete to fill in the rest of the field (and staying in the focus of the field.)

推荐答案

唯一万无一失的方法,我知道的,始终抓住所有的变化,不管他们是如何做的是使用的setInterval。这有点CPU密集型的,虽然如此,你可能想尽量保持选择比这更微乎其微。

The only foolproof way I know of to always catch ALL changes no matter how they're done is to use a setInterval. This is somewhat CPU intensive, though, so you probably want to try to keep the selector more minimal than this.

setInterval( function() {
    $('input').each( function() {
        if ($(this).val() != $(this).attr("_value")) {
            // Save the new value
            $(this).attr("_value", $(this).val());

            // TODO - Handle the changed value
        }
    });
}, 100);