簡體   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