私のユーザーは、フォームを送信する代わりに、Enter キーを押したときに、このフォームにフォーカスを移動させたいと考えています。onkeypress
Enter キーが押されたときにフォーカスを変更するハンドラーをテスト入力に追加しました。
以下のコードでは、keydown
関数がフォーカスを変更すると、カーソルが新しいテキスト ボックスにジャンプしますが、Enter キーをもう一度押したかのようにフォームが送信されます。イベントハンドラで false を返すとイベントが渡されないと思ったのですが、そうではないようです。Chrome と Firefox の両方でこの動作を確認しました。
誰かが私に欠けているものを教えてもらえますか?
<form action="" name="confirmation" method="post">
<fieldset>
<div class="clearfix">
<label for="id_event_fuel_amount">Quantity:</label>
<div class="input">
<input type="text" id="event_fuel_amount_id" name="event_fuel_amount" onkeypress="keydown(event, 'event_purchase_amount_id')" />
</div>
</div>
<div class="clearfix">
<label for="id_event_purchase_amount">Sale:</label>
<div class="input">
<input type="text" id="event_purchase_amount_id" name="event_purchase_amount" />
</div>
</div>
<input type="hidden" name="state" value="3" id="id_state" />
<input type="hidden" name="time_stamp" value="2011-09-24 14:34:06" id="id_time_stamp" />
<input type="hidden" name="purchase" value="66" id="id_purchase" />
<input type="submit" value="Save"/>
</fieldset>
</form>
<script>
function keydown(e,s){
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
if (code==13){
document.getElementById(s).focus();
return false;
}
}
</script>