jQuery可以检查输入内容是否已更改? [英] Can jQuery check whether input content has changed?

查看:88
本文介绍了jQuery可以检查输入内容是否已更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能以某种方式将javascript(jQuery是最好的)事件绑定到更改表单输入值?

Is it possible to bind javascript (jQuery is best) event to "change" form input value somehow?

我知道 .change()方法,但在你(光标)离开之前它不会触发( s)输入字段。我还考虑过使用 .keyup()方法,但它也会对箭头键做出反应,依此类推。

I know about .change() method, but it does not trigger until you (the cursor) leave(s) the input field. I have also considered using .keyup() method but it reacts also on arrow keys and so on.

我只需触发一个动作每次输入中的文字发生变化,即使它只有一个字母更改。

I need just trigger an action every time the text in the input changes, even if it's only one letter change.

推荐答案

有一个简单的解决方案,即HTML5 输入事件。在< input type =text> 元素的所有主要浏览器的当前版本中都支持它,IE<< 9.有关详细信息,请参阅以下答案:

There is a simple solution, which is the HTML5 input event. It's supported in current versions of all major browsers for <input type="text"> elements and there's a simple workaround for IE < 9. See the following answers for more details:

  • jQuery keyboard events
  • Catch only keypresses that change input?

示例(IE除外) < 9:请参阅上面的链接以获取解决方法):

Example (except IE < 9: see links above for workaround):

$("#your_id").on("input", function() {
    alert("Change to " + this.value);
});

这篇关于jQuery可以检查输入内容是否已更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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