简体   繁体   English

关于按键=在文本框中输入

[英]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 我有上面的代码,如果我单击其他位置,它将把焦点设置为weight_id_1

However I would like to actually inline with the text field to actually focus on weight_id_1 on keypress = enter 但是我想实际上与文本字段内联,以便实际上专注于keypress上的weight_id_1 = enter

How do I achieve it. 我该如何实现。

Thanks 谢谢

@clement's answer is perfectly right; @clement的答案是完全正确的; 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. 但是,使用keyup事件是一个小的改进,因为只要按下该键,它仅触发一次,而不触发多次。 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 : jQuery keypress事件的示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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