The following code works. It allows you to press the enter and escape buttons. Here is a jsFiddle

$(document).on('keydown', function(e){
    if (e.which == 13){
        // Enter Key pressed
    }
});
$(document).on('keydown', function(e){
    if (e.which == 27){
        // Esc Key pressed
    }
});

My question is how would I remove the binding for just the enter key?

有帮助吗?

解决方案

e.preventDefault(), and return false; in the function

$(document).on('keydown', function(e){
    if (e.which == 13){
        e.preventDefault();
        return false;
    }
});

or if you mean just the event binding

$(document).off('keydown');

if you mean just for that particular binding you can set a namespace

$(document).on('keydown.mykeydown'...
$(document).off('keydown.mykeydown');
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top