keyup无法在Android上运行Chrome

时间:2013-07-31 15:02:39

标签: android jquery onkeyup

我正在使用bootstrap typeahead。

这取决于这个jQuery代码:

el.on('keyup', doSomething() )

在Windows上的Chrome上运行正常。在Android上的Chrome上它没有。 keyup事件永远不会被触发。它所绑定的元素肯定是焦点。

这似乎是最近的发展。

Chrome 28.0.1500.64 Android 4.1.2 SGP321 Build / 10.1.1.A.1.307

由于

- Justin Wyllie

2 个答案:

答案 0 :(得分:3)

我今天早些时候遇到过同样的问题。 android chrome如何不支持这些关键事件!我假设你现在已经找到了解决方法,但这是我现在提出的修复方法。

function newKeyUpDown(originalFunction, eventType) {
    return function() {
        if ("ontouchstart" in document.documentElement) { // if it's a touch device, or test here specifically for android chrome
            var $element = $(this), $input = null;
            if (/input/i.test($element.prop('tagName')))
                $input = $element;
            else if ($('input', $element).size() > 0)
                $input = $($('input', $element).get(0));

            if ($input) {
                var currentVal = $input.val(), checkInterval = null;
                $input.focus(function(e) {
                    clearInterval(checkInterval);
                    checkInterval = setInterval(function() {
                        if ($input.val() != currentVal) {
                            var event = jQuery.Event(eventType);
                            currentVal = $input.val();
                            event.which = event.keyCode = (currentVal && currentVal.length > 0) ? currentVal.charCodeAt(currentVal.length - 1) : '';
                            $input.trigger(event);
                        }
                    }, 30);
                });
                $input.blur(function() {
                    clearInterval(checkInterval);
                });
            }
        }
        return originalFunction.apply(this, arguments);
    }
}
$.fn.keyup = newKeyUpDown($.fn.keyup, 'keyup');
$.fn.keydown = newKeyUpDown($.fn.keydown, 'keydown');

答案 1 :(得分:1)

很抱歉这样说但是keyup / keydown事件对于android中的chrome浏览器不起作用。 还有其他人在过去1年中报告了此问题(HereHere)并且尚未修复。所以开发人员最好避免使用这些事件,直到它得到修复。