jQuery textbox change event

前端 未结 6 1189
孤城傲影
孤城傲影 2020-12-09 09:29

Does text input element not have a change event? When I attach a change event handler to a text input it is not being fired. Keyup is fired, but keyup is not sufficient for

相关标签:
6条回答
  • 2020-12-09 09:30

    You can achieve it:

     $(document).ready(function(){              
         $('#textBox').keyup(function () {alert('changed');});
     });
    

    or with change (handle copy paste with right click):

     $(document).ready(function(){              
         $('#textBox2').change(function () {alert('changed');});
     });
    

    Here is Demo

    0 讨论(0)
  • 2020-12-09 09:34

    I have found that this works:

    $(document).ready(function(){
        $('textarea').bind('input propertychange', function() {
            //do your update here
        }
    
    })
    
    0 讨论(0)
  • 2020-12-09 09:35
    $('#input').on("input",function () {alert('changed');});
    

    works for me.

    0 讨论(0)
  • 2020-12-09 09:45

    The HTML4 spec for the <input> element specifies the following script events are available:

    onfocus, onblur, onselect, onchange, onclick, ondblclick, onmousedown, onmouseup, onmouseover, onmousemove, onmouseout, onkeypress, onkeydown, onkeyup

    here's an example that bind's to all these events and shows what's going on http://jsfiddle.net/pxfunc/zJ7Lf/

    I think you can filter out which events are truly relevent to your situation and detect what the text value was before and after the event to determine a change

    0 讨论(0)
  • 2020-12-09 09:46

    The change event only fires after the input loses focus (and was changed).

    0 讨论(0)
  • 2020-12-09 09:48

    There is no real solution to this - even in the links to other questions given above. In the end I have decided to use setTimeout and call a method that checks every second! Not an ideal solution, but a solution that works and code I am calling is simple enough to not have an effect on performance by being called all the time.

    function InitPageControls() {
            CheckIfChanged();
        }
    
        function CheckIfChanged() {
            // do logic
    
            setTimeout(function () {
                CheckIfChanged();
            }, 1000);
        }
    

    Hope this helps someone in the future as it seems there is no surefire way of acheiving this using event handlers...

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