Для нажатия клавиши (например, клавиши "a") событие keydown
запускается до того, как к текстовому вводу добавляется "a".Вы можете определить тип события и определить, является ли свойство события .keyCode
(или .which
) ключом в области печати, и попытаться предсказать, что .val()
может вернуть после события keyup
, но это плохая идея .
this.$input.bind('keydown keyup', function(e) {
var val = this.$input.val();
if (e.type == 'keydown') {
// Here is where you would check to see if e.keyCode was a printable
// character. Note that key codes differ from ASCII codes. For
// example, the "a" key will return key code 65, which is actually
// the ASCII code for "A", so you would need to check for the presence
// of shift keys and such. Again, this is a bad idea :)
val += String.fromCharCode(e.keyCode);
}
if (val == …) { … }
}