1
$("#inputField").keyup(function(event) {
  alert(event.keyCode);
  alert(event.charCode);
  alert(event.which);
  alert(String.fromCharCode(event.keyCode));
});
  • If I press A (lowercase "a"), output will be: 65, 0, 65, A
  • If I press Shift+A, the output still same: 65, 0, 65, A

Can someone teach me how to get small letter a when I key in a and get capital letter A when I key in A?

4

2 回答 2

1

您可以改用keypress事件:

$("#inputField").keypress(function(e) {
    alert(String.fromCharCode(e.which));
});​

PS:以下keyup文档:

为了捕捉实际的文本输入, .keypress() 可能是更好的选择。

于 2012-09-17T16:24:18.437 回答
0
$("#inputField").keyup(function(event) {
    var code = event.which;
    if(code >= 65 && code <= 90) {
        alert( String.fromCharCode( event.which + 32 ) );
    }
});

工作样本

于 2012-09-17T16:38:47.710 回答