0

我有一个小问题,试图以编程方式找出一个字符的键码值。这就是我目前所拥有的。

var delimiter = ',';

//some where down the page

control.keyup(function(e)
{
    var key = delimiter .charCodeAt(0);
    if(e.keycode == key)
    {
       //do something
    }
}  

因此,当我按下键盘上的“,”键时,其值为 44,而 e.keycode 为 188。如何找出变量分隔符的键码值?

4

1 回答 1

2

The keyup event returns a keycode not an ASCII code. If you switch to the keypress event you can retreive the ASCII code. This should match the value received by charCodeAt which returns the unicode value of a character, which happens to align with the ASCII code for the first 128 characters. See this reference.

var delimiter = ',';
var key = delimiter.charCodeAt(0);
document.getElementById("test").onkeypress = function(e){
    if((e.keyCode || e.which) == key){
       alert("Cat's out of the bag! OHHH YEAH!");
    }
};
于 2013-06-13T08:58:25.983 回答