keyup not working on Chrome on Android

前端 未结 2 728
Happy的楠姐
Happy的楠姐 2021-01-13 05:14

I am using bootstrap typeahead.

It depends on this jQuery code to work:

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

On Chrome on Windows it wo

2条回答
  •  不知归路
    2021-01-13 05:32

    I came across this same problem earlier today. How can android chrome not support these key events! I assume you've found a workaround by now, but here's a fix that I came up with for now.

    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');
    

提交回复
热议问题