简体   繁体   English

keyPress 事件未在 Android 手机中触发

[英]keyPress event not firing in Android mobile

I am using backbone,marionette for my Application.I used same code for both desktop and mobile but keypress not working in mobile .I made a Jsfiddle for testing.我正在为我的应用程序使用backbone,marionette 。我在desktop and mobile使用了相同的代码,但keypressmobile不起作用。我做了一个Jsfiddle进行测试。

If you open this link in mobile event not firing,If you open in desktop it's firing.How can I resolve this.如果您在mobile事件中打开此链接不会触发,如果您在desktop打开它会触发。我该如何解决这个问题。

can anyone help me.谁能帮我。

Thanks.谢谢。

Chrome mobile doesn't support keypress events properly for now. Chrome 移动版暂时无法正确支持按键事件。 There is a long standing bug for this:这有一个长期存在的错误:

https://code.google.com/p/chromium/issues/detail?id=118639 https://code.google.com/p/chromium/issues/detail?id=118639

I believe it should be fixed in v38.我相信它应该在 v38 中修复。 The bug report was closed as "won't fix".错误报告被关闭为“不会修复”。

I will suggest two events on input box.我会在输入框上建议两个事件。

This will work on desktop webbrowser.这将适用于桌面网络浏览器。

1) onkeypress = return isNumberKey(event); 1) onkeypress = return isNumberKey(event);

function isNumberKey(e)
{
    var evt = e || window.event;

    if(evt) 
    { 
        var charCode = evt.keyCode || evt.which; 
    }
    else 
    { 
        return true; 
    }

    if((charCode > 47 &&  charCode < 58) || charCode == 9 || charCode == 8 || charCode ==46 || charCode ==37 || charCode==39)
    { 
        return true; 
    }

    return false;
}

This will work for mobile这将适用于移动

2) onkeyup="numberMobile(event);" 2) onkeyup="numberMobile(event);"

function numberMobile(e){
    e.target.value = e.target.value.replace(/[^\d]/g,'');
    return false;
}

Apply both event on the input box and it will work.Only drawback is,It make it slow.But for now we have to live with it.在输入框上应用这两个事件,它会起作用。唯一的缺点是,它使它变慢。但现在我们必须忍受它。

There is one issue woth this solution.Left navigation is not working.I will update more appropriate solution soon.此解决方案存在一个问题。左导航不起作用。我将尽快更新更合适的解决方案。

似乎keypress事件已被弃用(请参阅https://developer.mozilla.org/en-US/docs/Web/Events/keypress ),最好在所有浏览器放弃支持之前使用keydown事件。

Tested on browserstack.在浏览器堆栈上测试。 Seems to be working to me.似乎对我有用。 Here's a link to your gist:这是指向您的要点的链接:

http://www.browserstack.com/start#os=android&os_version=4.1&device=Samsung+Galaxy+S+III&zoom_to_fit=true&full_screen=true&url=http%3A%2F%2Fjsfiddle.net%2F33Snz%2F3%2Fembedded%2Fresult%2F&speed=1&start=true http://www.browserstack.com/start#os=android&os_version=4.1&device=Samsung+Galaxy+S+III&zoom_to_fit=true&full_screen=true&url=http%3A%2F%2Fjsfiddle.net%2F33Snz%2F3%2Fembedded%2Fresult%2F&speed =1&开始=真

Can you let know what specific andriod device and browser you testing in.你能知道你测试的是什么特定的安卓设备和浏览器吗?

Finally I found a problem,but it won't work in opera.Instead of writing the code for numeric fields.I just added an attribute type='number' to input field in html file.Now only numeric keypad is coming.So it satisfies my requirements.Here is the JsFiddle最后发现了一个问题,但是在opera中不行满足我的要求。这是JsFiddle

html code: html代码:

<input type='number'/>

With my experience i would like to share my knowledge,以我的经验,我想分享我的知识,

Desktop Browsers桌面浏览器

<input type="number">
  1. Safari - allows Chars too (may be depends on version) Safari - 也允许字符(可能取决于版本)
  2. maxlength not supported.不支持最大长度。
  3. Cursor position is unable to detect.无法检测光标位置。
  4. @keypress event detected.检测到@keypress 事件。

<input type="text">
  1. works as expected.按预期工作。

<input type="tel">
  1. works as expected.按预期工作。

Android Mobile Browser (Chrome)安卓手机浏览器 (Chrome)

<input type="number">
  1. Opens numeric keypad打开数字小键盘
  2. @keypress event detected.检测到@keypress 事件。
  3. Cursor position is unable to detect.无法检测光标位置。

 <input type="text">
  1. @keypress event not even detected.甚至没有检测到@keypress 事件。
  2. maxlength not working properly. maxlength 无法正常工作。

 <input type="tel">
  1. @keypress event is detected.检测到@keypress 事件。
  2. maxlength supported.支持最大长度。
  3. Cursor position in input field can be detected.可以检测输入字段中的光标位置。

Note: For detecting cursor position I used,注意:为了检测我使用的光标位置,

let caretPos = e.target.selectionStart

Let me know is there are any ways to detect cursor position in input[type="number"] .让我知道有什么方法可以检测input[type="number"] 中的光标位置。


Suggestion建议

For Desktop browsers, use input[type="text"] to handle cases like keypress event and replace some values using cursor position.对于桌面浏览器,使用input[type="text"]处理诸如按键事件之类的情况并使用光标位置替换一些值。

For Android browsers, use input[type="tel"] to handle same cases mentioned above.对于 Android 浏览器,使用input[type="tel"]来处理上述相同的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM