jQuery: textbox keyup firing twice

前端 未结 8 1856
南笙
南笙 2021-01-11 16:09

I\'m having a textbox and assigned the following function (it\'s the only function assigned):

txt.bind(\"keyup\",function(event){
    if(event.keyCode==13)
          


        
相关标签:
8条回答
  • 2021-01-11 16:40

    i found out by myself - txt.bind was assigned twice to the textbox so it fired twice. is this a bug? i thought binding a function will always fire just once .. hmm

    0 讨论(0)
  • 2021-01-11 16:43

    I'm embarrassed to even mention this, but I was typing a capital letter into my textbox and so the release of my shift key was causing the supposed "second firing" of the keyup event. Turns out it wasn't a second firing at all, but it just fooled me into thinking it was for while. At first I thought the unbind trick mentioned above simply wasn't working for me, but it was. I hope this helps others like me that might have done the same thing. Now I simply make sure I always check that its not just the shift key being released in the handling of my event and all is well again.

    0 讨论(0)
  • 2021-01-11 16:47

    could it be possible that your html-element is contained twice within txt?

    it would be helpful if you would provide the html and the assigning javascript-code.

    0 讨论(0)
  • 2021-01-11 16:50

    I'm having the same issue - keyup and keydown events are being fired twice though their handlers are bound only once and I'm definitely attaching them to only one element.

    The interesting point is that this behavior can be observed only in Internet Explorer and in just one special case where my webapp is displayed in an embedded ActiveX control (CLSID_InternetExplorer). OS: Windows 7, IE 8, embedded ActiveX control is running in IE7 compatibility mode.

    I've found a workaround however - process the jQuery's keypress event, which made also more semantic sense in my webapp.

    0 讨论(0)
  • 2021-01-11 16:52

    I know it's been quite awhile, but I'm surprised nobody suggested:

    txt.unbind();
    txt.bind("keyup",function(event){
        if(event.keyCode==13)
        {
            var nu = $("#debug").html();
            nu+="<br>enter";
            $("#debug").html(nu);
        }
    });
    

    If somehow txt got bound already, calling unbind before your new bind should fix it. Of course it's preferable to figure out why it's being bound twice. This just happened to me, because I accidentally had my JavaScript file included twice. Calling unbind helped me determine that was the problem.

    0 讨论(0)
  • 2021-01-11 16:54

    I had the same problem. Though i could not find a solution yet, i found the reason why it is triggering twice.

    I am handing the event if the entered text has "@".For this i am listing each key stroke. To enter '@' you need two key precesses (@+shift) and that is the reason it is triggering twice.

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