Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

javascript - 如何检测文本框的内容已更改(How to detect a textbox's content has changed)

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.(我可以使用keyup方法,但是它也将检测不生成字母的击键,例如箭头键。) I thought of two methods of doing this using the keyup event:(我想到了使用keyup事件执行此操作的两种方法:)
  1. Check explictly if the ascii code of the pressed key is a letter\backspace\delete(仔细检查所按键的ASCII码是否为字母\退格\删除)
  2. Use closures to remember what was the text in the textbox before the key stroke and check whether this has changed.(使用闭包来记住按键之前文本框中的文本,并检查是否已更改。)

Both look kinda cumbersome.(两者看起来都很麻烦。)

  ask by olamundo translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...