问题
This is my input form:
<input type="text" id="word" />
I basically want a Javascript function to execute when the person presses enter in the text box. I'm sorry if this is an obvious question, I couldn't find an existing answer that matched my needs.
回答1:
keyup() is your answer:
$('#word').keyup(function(e) {
if(e.which == 13) {
// Do your stuff
}
});
e.which
returns the scan/character code of the pressed key. 13
is for ENTER
.
You can find other codes here.
回答2:
$('#word').on('keyup', function(e){
if(e.which === 13){
// User pressed enter
}
});
Testing the event which
property will tell you what key the user has pressed.which
is normalized by jQuery, so you won't have any cross-browser issues.
来源:https://stackoverflow.com/questions/13793550/running-a-function-when-enter-is-pressed-in-a-text-box