Best way to track onchange as-you-type in input type=“text”?

后端 未结 16 1147
天命终不由人
天命终不由人 2020-11-22 08:42

In my experience, input type=\"text\" onchange event usually occurs only after you leave (blur) the control.

Is there a way to

相关标签:
16条回答
  • 2020-11-22 08:47

    Update:

    See Another answer (2015).


    Original 2009 Answer:

    So, you want the onchange event to fire on keydown, blur, and paste? That's magic.

    If you want to track changes as they type, use "onkeydown". If you need to trap paste operations with the mouse, use "onpaste" (IE, FF3) and "oninput" (FF, Opera, Chrome, Safari1).

    1Broken for <textarea> on Safari. Use textInput instead

    0 讨论(0)
  • 2020-11-22 08:48

    Below code works fine for me with Jquery 1.8.3

    HTML : <input type="text" id="myId" />

    Javascript/JQuery:

    $("#myId").on('change keydown paste input', function(){
          doSomething();
    });
    
    0 讨论(0)
  • 2020-11-22 08:50

    onkeyup happens after you type for example I press t when I lift the finger the action happens but on keydown the action happens before I digit the character t

    Hope this is helpful for someone.

    So onkeyup is better for when you want to send what you just typed now.

    0 讨论(0)
  • 2020-11-22 08:50

    2018 here, this is what I do:

    $(inputs).on('change keydown paste input propertychange click keyup blur',handler);
    

    If you can point out flaws in this approach, I would be grateful.

    0 讨论(0)
  • 2020-11-22 08:52

    You could use the keydown, keyup and keypress events as well.

    0 讨论(0)
  • 2020-11-22 08:54

    If you use ONLY Internet Explorer, you can use this:

    <input type="text" id="myID" onpropertychange="TextChange(this)" />
    
    <script type="text/javascript">
        function TextChange(tBox) {
            if(event.propertyName=='value') {
                //your code here
            }
        }
    </script>
    

    Hope that helps.

    0 讨论(0)
提交回复
热议问题