如果输入为空,jQuery输入事件不会触发

时间:2022-03-18 20:27:41

I have a jQuery event handler that reacts to every change in an

我有一个jQuery事件处理程序,它对每一个变化做出反应。

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

element:

元素:

$("#myId").bind('input', function (e) {    
        // Do Stuff
    });

This works perfectly, except when the input in #myId is empty (e.g. there was just 1 character in the input box, and I remove it with Backspace). If the input has become empty, the event does not fire. If I then enter a character, the event fires.

除了#myId中的输入为空(例如,在输入框中只有一个字符,我使用退格删除它)之外,这一操作非常有效。如果输入变为空,则不会触发事件。如果我输入一个字符,则事件将触发。

Is this a known issue? Am I doing something wrong? Any way to get an event when the input goes empty?

这是一个已知的问题吗?我做错什么了吗?有什么方法可以在输入为空时获取事件?

Update

更新

The event does not fire AT ALL when Backspace is entered, at least in IE9.

至少在IE9中,当返回空间被输入时,事件根本不会触发。

1 个解决方案

#1


8  

as far as I know, "input" is not the event you want to use. Since "input" is analogous to "change", it tends to produce a bad result when your string approaches 0 chars, use keyup instead

就我所知,“input”不是您想要使用的事件。由于“input”类似于“change”,当您的字符串接近0字符时,它往往会产生糟糕的结果,因此使用keyup代替

$("input#myId").bind('keyup', function (e) {    
    // Do Stuff
});

#1


8  

as far as I know, "input" is not the event you want to use. Since "input" is analogous to "change", it tends to produce a bad result when your string approaches 0 chars, use keyup instead

就我所知,“input”不是您想要使用的事件。由于“input”类似于“change”,当您的字符串接近0字符时,它往往会产生糟糕的结果,因此使用keyup代替

$("input#myId").bind('keyup', function (e) {    
    // Do Stuff
});