Why does jQuery work to work in a window, but not in a text box?

Why does it work:

$(window).keydown(function(event){
    alert(event.keyCode);
});

but not this:

$('#ajaxSearchText').keydown(function(event){
    alert(event.keyCode);
});

I am testing Firefox 3. Interestingly, none of them work in IE7.

+3
source share
6 answers

This is verified in Chrome, IE7, and Firefox 3.0.3. It works as it should. JQuery version 1.2.6.

<html> 
  <head> 
    <script type="text/javascript" src="jquery-1.2.6.js"></script> 
    <script type="text/javascript"> 
      $(function() 
      {
        $("#ajaxSearchText").keydown(function(event)
        {
          alert(event.keyCode);
        });
      });
    </script> 
  </head> 
  <body> 
    <input type="text" id="ajaxSearchText"></input>
  </body> 
</html> 
+7
source

To fix issues in IE6 and IE7, try this ...

$(function() 
{
    $(document).keydown(function(event){
        alert(event.keyCode);
    });
});

Attaching the event to $(document)seems magical here.

Your first bit of code should really work in IE too. This seems to be a bug in jQuery, which I hope will be fixed soon ...

Here is the link to the jQuery bug report. https://bugs.jquery.com/ticket/3614

+4
source

keydown/keyup/keyboard hotkeys jQuery.

, . ... http://code.google.com/p/js-hotkeys/

, . , !

+3

$('#ajaxSearchText').keyup(function(event){
    alert(event.keyCode);
});

.

+1
$('#searchInput').keydown(function() {
    alert('testing');
});

. , :

$(function()
{
    $('#searchInput').keydown(function() {
        alert('testing');
    });
});

.

, .

0

Source: https://habr.com/ru/post/1698093/


All Articles