未在 Android 浏览器上引发 JavaScript 按键事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21055270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:54:33  来源:igfitidea点击:

JavaScript keypress event not raised on Android browser

javascriptandroidjquerykeypresshybrid-mobile-app

提问by alex.mironov

I have created a simple code to handle keypressevent:

我创建了一个简单的代码来处理keypress事件:

var counter = 0;        
$('input').on('keypress', function () {
    $('div').text('key pressed ' + ++counter);
});

JSFiddle.

JSFiddle。

But keypress event handler is not raised on mobile browser (Android 4+, WindowsPhone 7.5+). What could be the issue?

但是在移动浏览器(Android 4+、WindowsPhone 7.5+)上不会引发按键事件处理程序。可能是什么问题?

采纳答案by HDT

Try keyupwill help you

尝试keyup会帮助你

var counter = 0;        
$('input').on('keyup', function () {
    $('div').text('key up ' + ++counter);
});

回答by Adam Hughes

I believe keypressis deprecated now. You can check in the Dom Level 3 Spec. Using keydownor keyupshould work. The spec also recommends that you should use beforeinputinstead of keypressbut I'm not sure what the support of this is.

我相信keypress现在已被弃用。您可以查看Dom Level 3 Spec。使用keydownkeyup应该可以工作。该规范还建议,你应该使用beforeinput代替按键,但我不知道这样的支持。

回答by Chakradhari Jamili

$(document).ready(function() {
  var pattForZip = /[0-9]/;
  $('#id').on('keypress input', function(event) {
    if(event.type == "keypress") {
      if(pattForZip.test(event.key)) {
        return true;
      }
      return false;
    }
    if(event.type == 'input') {
      var bufferValue = $(this).val().replace(/\D/g,'');
      $(this).val(bufferValue);
    }
  })
})

回答by Gsvp Nagaraju

Yes, some android browser are not supporting keypress event, we need use to only keydown or keyup but will get different keycodes, to avoiding different key codes use the following function to get the keycode by sending char value.

是的,一些android浏览器不支持keypress事件,我们只需要使用keydown或keyup就会得到不同的keycode,为了避免不同的keycode,使用下面的函数通过发送char值来获取keycode。

Eg:

例如:

function getKeyCode(str) {
  return str && str.charCodeAt(0);    
}
function keyUp(){
 var keyCode = getKeyCode("1"); 
}

回答by Torben

Use jQuery's input event, like this:

使用 jQuery 的 input 事件,像这样:

$( 'input' ).on( 'input', function() {
    ...
} );

With this you can't use e.which for determining which key was pressed, but I found a nice workaround here: http://jsfiddle.net/zminic/8Lmay/

有了这个,你不能使用 e.which 来确定按下了哪个键,但我在这里找到了一个很好的解决方法:http: //jsfiddle.net/zminic/8Lmay/

回答by Devang Kamdar

I think it is bad idea to use other events in place of 'keypress'. What you need to do is just include a jQuery file into your project. A file named jQuery.mobile.js or quite similar (ex. jQuery.ui.js) of any version can help you.

我认为使用其他事件代替“按键”是个坏主意。您需要做的只是在您的项目中包含一个 jQuery 文件。一个名为 jQuery.mobile.js 或非常相似(例如 jQuery.ui.js)的任何版本的文件都可以为您提供帮助。

You can download it from : https://jquerymobile.com/download/

您可以从以下网址下载:https: //jquerymobile.com/download/