如何检测文本框的内容已更改 [英] How to detect a textbox's content has changed

查看:126
本文介绍了如何检测文本框的内容已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文本框的内容发生变化时进行检测。我可以使用keyup方法,但这也会检测不生成字母的击键,如箭头键。我想到了使用keyup事件执行此操作的两种方法:

I want to detect whenever a textbox's content has changed. I can use the keyup method, but that will also detect keystrokes which do not generate letters, like the arrow keys. I thought of two methods of doing this using the keyup event:


  1. 如果按下的键的ascii代码是letter,则显式检查backspace\delete

  2. 使用闭包记住键击之前文本框中的文本是什么,并检查是否已更改。

两者看起来都很麻烦。

Both look kinda cumbersome.

推荐答案

开始观察'输入'事件而不是'更改'。

Start observing 'input' event instead of 'change'.

jQuery('#some_text_box').on('input', function() {
    // do your stuff
});

...这很干净,但可以进一步扩展到:

...which is nice and clean, but may be extended further to:

jQuery('#some_text_box').on('input propertychange paste', function() {
    // do your stuff
});

这篇关于如何检测文本框的内容已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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