简体   繁体   中英

Regarding On Key Press = Enter in textfield

Game Name <input type="text" maxlength="20" name="game_name" onblur="$('#weight_id_1').focus();"> 

I have the code above, if I click elsewhere, it will set focus to weight_id_1

However I would like to actually inline with the text field to actually focus on weight_id_1 on keypress = enter

How do I achieve it.

Thanks

@clement's answer is perfectly right; however, a small improvement would be to use the keyup event because it fires only once rather than many times for as long as the key is depressed. also, if this is in a form, you should prevent default so it's not submitted

$('#text_field').on('keyup', function(e) { // fires only once per keypress
    e.preventDefault();
    e.which == 13 && $('#weight_id_1').focus();
});

Example of a jQuery keypress event :

$( "#your_text_field" ).keypress(function( event ) {
  if ( event.which == 13 ) { //13 is for enter
    $('#weight_id_1').focus();
  }      
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM