修改输入中的值时的Shot事件

Shot event when the value in an input is modified

本文关键字:Shot 事件 修改 输入      更新时间:2023-09-26

考虑以下内容:

<input type="text" id="foo" />

我需要的是,当input.#foo的值更改时,没有模糊,它会触发我的事件。问题来了:我不能使用onkey事件,因为输入通常由虚拟HTML组成的键盘接收。

我可以很容易地做到,用真正的键盘:

document.querySelector("#foo").addEventListener('keypress', function() {
  // Implementation
});

W3C建议使用onchange,但onchange只有在blur事件发生后才有效。我需要onkeypressonchange的混合,以便动态、按时修改,并知道它何时更改。您可以看到:http://jsfiddle.net/zuq733La/

JS Fiddle

使用textInput,它将检查添加的每个字符

document.querySelector("#foo").addEventListener('textInput', function () {
   alert('Value changed!');
});

我能想到的唯一方法(不使用blurchangekeypresskeyupkeydown事件)是使用setInterval():DEMO

var value=$('#foo').val();
setInterval(function(){
    if(value!=$('#foo').val()){
        value=$('#foo').val();
        //here you need to write what will happen if the value is changed
    }
},50);

在DEMO中,您可以看到,即使复制和粘贴文本(仅使用鼠标,即使不模糊输入),事件也会触发。