keyPress 事件未在 Android 手机中触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22473950/
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
keyPress event not firing in Android mobile
提问by user3279058
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 Jsfiddlefor testing.
我正在使用backbone,marionette
我的应用程序。我对两者都使用了相同的代码,desktop and mobile
但keypress
没有使用mobile
。我做了一个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.
谢谢。
回答by Quin
Chrome mobile doesn't support keypress events properly for now. There is a long standing bug for this:
Chrome 移动版暂时无法正确支持按键事件。这有一个长期存在的错误:
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 中修复。
回答by Innovation
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.
此解决方案存在一个问题。左导航不起作用。我将尽快更新更合适的解决方案。
回答by Kong
It seems like keypress
event has been deprecated (Refer to https://developer.mozilla.org/en-US/docs/Web/Events/keypress) and it is better to use keydown
event now before all browser drop support on it.
似乎keypress
event 已被弃用(请参阅https://developer.mozilla.org/en-US/docs/Web/Events/keypress),最好keydown
在所有浏览器放弃支持之前立即使用event。
回答by Ahmed Abbas
Tested on browserstack. Seems to be working to me. Here's a link to your gist:
在浏览器堆栈上测试。似乎对我有用。这是指向您的要点的链接:
Can you let know what specific andriod device and browser you testing in.
你能知道你测试的是什么特定的安卓设备和浏览器吗?
回答by user3279058
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里不行。我没有写数字字段的代码。我只是在html文件的输入字段中添加了一个属性type='number'。现在只有数字键盘来了。所以它满足我的要求。这是JsFiddle
html code:
html代码:
<input type='number'/>