在Android版Chrome中,键码始终为零 [英] keycode is always zero in Chrome for Android
问题描述
我需要在我的网站上检测自定义搜索框的键码,但Chrome上的键码始终返回为零(除退格键返回8外)。有没有其他人经历过这个,你是如何解决它的?我们的网站适用于除Chrome之外的所有移动浏览器,因为我们无法检测到非零的键码或charcode。
I need to detect the keycode for a custom search box on my website, but the keycode always returns as zero on Chrome for Android (except for backspace, which returns 8). Has anyone else experienced this, and how did you get around it? Our website works on all mobile browsers except Chrome for Android because we can't detect a non-zero keycode or charcode.
我在Android上运行Chrome 27.0.1453.90 4.1.2果冻豆。这个问题可以通过如下简单的东西来复制:
alert(event.keyCode);
I'm running Chrome 27.0.1453.90 on Android 4.1.2 Jelly Bean. The problem can be duplicated with something as simple as:
alert(event.keyCode);
推荐答案
以下解决方案 也为我工作。可能对其他人也有用。
below solution also work for me. might be useful for others also.
var getKeyCode = function (str) {
return str.charCodeAt(str.length - 1);
}
document.getElementById("a").onkeyup = function (e) {
var kCd = e.keyCode || e.which;
if (kCd == 0 || kCd == 229) { //for android chrome keycode fix
kCd = getKeyCode(this.value);
}
alert(kCd)
}
这篇关于在Android版Chrome中,键码始终为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!