1

I want to be able to listen to multiple button presses, like so:

$("#body").keypress(function(event) {

        if (event.which == 49) { //1
         //do something
        }
        if (event.which == 68) { //d 
             //do something else
        }

});

But I'm not able to intercept the "d" keypress. Any advice?

Cheers

4

1 回答 1

3

You could use a switch statement, permitting 100 (lowercase 'd') to fall through to 68 (uppercase 'D'):

$("#foo").on("keypress", function(e){
    switch( e.which ) {
        case 49 :
          alert( "You pressed a 1" );
          break;
        case 100:
        case 68 :
          alert( "You pressed a 'd'" );
    }
});​

Fiddle: http://jsfiddle.net/jonathansampson/CJKUh/

于 2012-06-03T23:24:36.700 回答