This is the code and browser screenshot. The first one is number, the second one is also number, but the third one is NaN.
I want to get a key combination, such as Alt 1...Alt 9 and then decrease the key value. Go to a number that corresponds to the value of the option of select
but it keeps reporting NaN
$(function(){
$(document).keydown(function(event){
if(event.keyCode <=57 && event.keyCode >= 49 && event.altKey){
var selectval = (event.keyCode - 48);
console.log(typeof(event.keyCode));
console.log(typeof(selectval));
console.log(selectval);
}
});
})
世界只因有你2017-05-19 10:32:37
var selectval = (event.KeyCode - 48); => var selectval = (event.keyCode - 48);
是KeyCode还是keyCode?
曾经蜡笔没有小新2017-05-19 10:32:37
There’s nothing wrong with writing it in native language
document.addEventListener('keydown',function(event){
if(event.keyCode <=57 && event.keyCode >= 49 && event.altKey){
var selectval = (event.keyCode - 48);
console.log(typeof(event.keyCode));
console.log(typeof(selectval));
console.log(selectval);
}
},false);
伊谢尔伦2017-05-19 10:32:37
Use event.ctrlKey, event.shiftKey, event.altKey to determine whether the ctrl key, shift key and alt key are pressed
过去多啦不再A梦2017-05-19 10:32:37
After changing several environments, this situation finally occurred. They are all the highest version of Google. I don’t know how there are several computers without undefined
The keyCode received by the new variable is undefined;
As for why I don’t understand it yet, can someone please explain the mechanism
过去多啦不再A梦2017-05-19 10:32:37
jQuery will help you with compatibility processing, please use event.which
代替event.keyCode
.
No need for jQuery:
// 获取用户单击键盘的“键值”
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;