keyup, keydown and keypress events not working on mobile

前端 未结 1 1555
清酒与你
清酒与你 2021-01-06 13:51

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.         


        
相关标签:
1条回答
  • 2021-01-06 14:46

    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>

    Using keydown (works as expected)

    jQuery(function($) { // DOM ready and $ alias secured
    
      $('#buscar-producto').on('keydown', function(e){
        alert( e.which );
      });
    
    });
    <input type="text" id="buscar-producto" placeholder="Buscar...">
    
    <script src="https://code.jquery.com/jquery-3.1.0.js"></script>

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