Frage

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?

War es hilfreich?

Lösung

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');
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top