本文介绍了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:
-
keydown
触发. -
e.keyCode
设置为所按下键的ASCII值. -
this.value
是''
(即在键入"a"之前相同). -
input
触发. -
e.keyCode
是undefined
. -
this.value
是'a'
.
keydown
fires.e.keyCode
is set to the ASCII value of the key pressed.this.value
is''
(i.e. the same before 'a' has been typed).input
fires.e.keyCode
isundefined
.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-键触发事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!