繁体   English   中英

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

[英]Regarding On Key Press = Enter in textfield

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

我有上面的代码,如果我单击其他位置,它将把焦点设置为weight_id_1

但是我想实际上与文本字段内联,以便实际上专注于keypress上的weight_id_1 = enter

我该如何实现。

谢谢

@clement的答案是完全正确的; 但是,使用keyup事件是一个小的改进,因为只要按下该键,它仅触发一次,而不触发多次。 另外,如果是表格形式,则应防止使用默认值,这样就不会提交

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

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