Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have been trying to get this to work but I don't know what's going on, the code I have:

$('#buscar-producto').on('keydown', function(e){
    console.log('hello');
    console.log(e.keyCode);
});

It works on computers, but not on mobile..

EDIT: I need to get the keyCode when a key is pressed...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.6k views
Welcome To Ask or Share your Answers For Others

1 Answer

keydown should work, but you could use the input event which seems to have undesired effects on Android mobile...
To get the code of the pressed key use the jQuery's normalized Event.which

Android Chrome tested:

Using input Event (e.which gives always 0 so it seems like a bug on android devices)

jQuery(function($) { // DOM ready and $ alias secured

  $('#buscar-producto').on('input', function(e){
    var key = e.which || this.value.substr(-1).charCodeAt(0);
    alert( key )
  });

});
<input type="text" id="buscar-producto" placeholder="Buscar...">

<script src="https://code.jquery.com/jquery-3.1.0.js"></script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...