keyup无法在Android上运行Chrome

我正在使用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

最佳答案
我今天早些时候遇到过同样的问题. 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');

相关文章

jQuery表单验证提交:前台验证一(图文+视频)
jQuery表单验证提交:前台验证二(图文+视频)
jQuery如何实时监听获取input输入框的值
JQuery怎么判断元素是否存在
jQuery如何实现定时重定向
jquery如何获取复选框选中的值