iOS 9-键触发事件未触发 [英] iOS 9 - keyup event not firing

查看:163
本文介绍了iOS 9-键触发事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 9中是否还有其他人无法触发keyup事件?

Is anybody else having problems with the keyup event in iOS 9 not firing?

只需一个简单的试验台即可为我复制问题.

Just a simple test bed replicates the issue for me.

<input id="txtInput" />

香草JS:

document.getElementById('txtInput').onkeyup = function () {
    console.log('keyup triggered');
}

jQuery:

$('#txtInput').on('keyup', function () {
    console.log('keyup triggered');
});

两枪都没有...

推荐答案

这不是很漂亮,但一种解决方法是绑定到keydown以捕获已按下的键,如果要获取已按下的键,则绑定input.值,包括键入的键:

It's not pretty, but a work around is to bind to keydown to capture which key has been pressed, and input if you want to obtain the value, including the key typed:

(function () {
    var keyCode;

    $('#txtInput')
        .on('keydown', function (e) {
            // value not updated yet
            keyCode = e.keyCode;

            // Enter key does not trigger 'input' events; manually trigger it
            if (e.keyCode === 13) $(this).trigger('input');
        })
        .on('input', function (e) {
            console.log(keyCode, this.value);
        });
}());

如果键入"a",则会发生以下情况:

If you type 'a' the following occurs:

  1. keydown触发.
  2. e.keyCode设置为所按下键的ASCII值.
  3. this.value''(即在​​键入"a"之前相同).
  4. input触发.
  5. e.keyCodeundefined.
  6. this.value'a'.
  1. keydown fires.
  2. e.keyCode is set to the ASCII value of the key pressed.
  3. this.value is '' (i.e. the same before 'a' has been typed).
  4. input fires.
  5. e.keyCode is undefined.
  6. this.value is 'a'.

如果按下回车键(13),也可以手动触发input事件;否则,您可以手动触发input事件.默认情况下,此键不会触发input.

You can also manually trigger an input event if the enter (13) key is pressed; input isn't fired by this key by default.

这篇关于iOS 9-键触发事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆