How to detect a textbox's content has changed

后端 未结 15 1897
猫巷女王i
猫巷女王i 2020-11-22 16:10

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 ke

15条回答
  •  情歌与酒
    2020-11-22 16:13

    Use closures to remember what was the text in the checkbox before the key stroke and check whether this has changed.

    Yep. You don't have to use closures necessarily, but you will need to remember the old value and compare it to the new.

    However! This still won't catch every change, because there a ways of editing textbox content that do not involve any keypress. For example selecting a range of text then right-click-cut. Or dragging it. Or dropping text from another app into the textbox. Or changing a word via the browser's spell-check. Or...

    So if you must detect every change, you have to poll for it. You could window.setInterval to check the field against its previous value every (say) second. You could also wire onkeyup to the same function so that changes that are caused by keypresses are reflected quicker.

    Cumbersome? Yes. But it's that or just do it the normal HTML onchange way and don't try to instant-update.

提交回复
热议问题