jQuery Event Keypress: Which key was pressed?

后端 未结 24 2087
半阙折子戏
半阙折子戏 2020-11-21 16:48

With jQuery, how do I find out which key was pressed when I bind to the keypress event?

$(\'#searchbox input\').bind(\'keypress\', function(e) {});
         


        
24条回答
  •  情书的邮戳
    2020-11-21 16:54

     // in jquery source code...
     if (!event.which && ((event.charCode || event.charCode === 0) ? event.charCode : event.keyCode)) {
         event.which = event.charCode || event.keyCode;
     }
    
     // So you have just to use
     $('#searchbox input').bind('keypress', function(e) {
         if (e.which === 13) {
             alert('ENTER WAS PRESSED');
         }
     });
    

提交回复
热议问题