html <input type=“text” /> onchange event not working

后端 未结 12 2515
-上瘾入骨i
-上瘾入骨i 2020-11-30 01:13

I am trying to do some experiment. What I want to happen is that everytime the user types in something in the textbox, it will be displayed in a dialog box. I used the

12条回答
  •  -上瘾入骨i
    2020-11-30 01:34

    A couple of comments that IMO are important:

    • input elements not not emitting 'change' event until USER action ENTER or blur await IS the correct behavior.

    • The event you want to use is "input" ("oninput"). Here is well demonstrated the different between the two: https://javascript.info/events-change-input

    • The two events signal two different user gestures/moments ("input" event means user is writing or navigating a select list options, but still didn't confirm the change. "change" means user did changed the value (with an enter or blur our)

    • Listening for key events like many here recommended is a bad practice in this case. (like people modifying the default behavior of ENTER on inputs)...

    • jQuery has nothing to do with this. This is all in HTML standard.

    • If you have problems understanding WHY this is the correct behavior, perhaps is helpful, as experiment, use your text editor or browser without a mouse/pad, just a keyboard.

    My two cents.

提交回复
热议问题