WebフォームでEnterキーを押したときにフォーカスを移動させたい
いやTabキー押せよ、って思うけど仕方ない。
やり方
$('form').on('keydown', 'input, button, select', function(e) { if (e.keyCode == 13) { if ($(this).attr("type") == 'submit') return; var form = $(this).closest('form'); var focusable = form.find('input, button[type="submit"], select, textarea') .not('[readonly]').filter(':visible'); if (e.shiftKey) { focusable.eq(focusable.index(this) - 1).focus(); } else { var next = focusable.eq(focusable.index(this) + 1); if (next.length) { next.focus(); } else { focusable.eq(0).focus(); } } e.preventDefault(); } });
ポイント
textareaとsubmitボタンにフォーカスがある場合は移動せずに本来の動作(textareaなら改行、submitならsubmit)を行う。