プロが教えるわが家の防犯対策術!

質問させて頂きます。

下記の様にenterキーを押してtab移動しようと考えています。

<form name="form">
<input type="text" id="id1" onkeypress="if(code == 13)document.form.item('id2').focus();">
<input type="text" id="id2" onkeypress="if(code == 13)document.form.item('id3').focus();">
<input type="button" id="id3" value="ボタン">
</form>

ここで、textフォームからボタンへ移動する場合、自動的にボタンが押されてしまいます。
(エンターキーでの移動のため)

ボタンが押されないような方法はあるでしょうか。アドバイスお願い致します。

A 回答 (2件)

以前なんかで書いたの転載します。

(シフトキー対応)

<script>
try{
document.addEventListener('keydown',function(e){keydownfunc(e)},true);
}catch(e){
document.attachEvent('onkeydown',function(e){keydownfunc(e)});
}
function keydownfunc(e){
var t = (e.srcElement || e.target);
if(t.nodeName!="INPUT" && t.nodeName!="SELECT") return false;
if(e.keyCode==13){
f=t.form;
for(var i=0;i<f.length;i++){
if(t==f[i]){
if(e.shiftKey){
var n=(i==0)?f.length-1:i-1;
}else{
var n=(i==f.length-1)?0:i+1;
}
f[n].focus();
break;
}
}
}
}
</script>

<form>
<input type="text">
<input type="text">
<select>
<option>test1</option>
<option>test2</option>
<option>test3</option>
</select>
<input type="button" value="ボタン">
</form>
    • good
    • 0
この回答へのお礼

ご回答ありがとうございました。

試してみたいと思います。

失礼します。

お礼日時:2010/12/10 19:40

onkeypress="if(code == 13)document.form.item('id2').focus(), retu

quot;
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!