如果输入为空,则jQuery输入事件不会触发 [英] jQuery Input Event does not Fire if Input is Empty

查看:116
本文介绍了如果输入为空,则jQuery输入事件不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jQuery事件处理程序,它对

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

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

元素:

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

除非输入 #myId 为空(例如输入框中只有1个字符,我用Backspace删除它)。如果输入变空,则事件不会触发。如果我然后输入一个字符,事件就会触发。

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.

这是一个已知问题吗?难道我做错了什么?输入变空时以何种方式获取活动?

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

更新

输入Backspace时,事件不会触发,至少在IE9中。

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

推荐答案

据我所知,输入不是你想要使用的事件。由于输入类似于更改,当您的字符串接近0个字符时,它往往会产生错误的结果,使用 keyup 而不是

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#myId").bind('keyup', function (e) {    
    // Do Stuff
});

这篇关于如果输入为空,则jQuery输入事件不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆