javascript:
function change_field(evt, id, id_name){
var d = "#"+id_name+(id+1); //下一個欄位
var u = "#"+id_name+(id-1); //上一個欄位
var r = "#txt"+id; //右邊欄位
var l = "#txt_r"+id; //左邊欄位
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode == 40){ //向下鍵
$(d).focus();
}else if (charCode == 38){ //向上鍵
$(u).focus();
}else if (charCode == 37){ //向左鍵
$(l).focus();
}else if (charCode == 39){ //向右鍵
$(r).focus();
}
}
Html:
欄位的排列需有規則,欄位給id,用id來控制
<input type="text" name="txt1" id="txt1" onkeydown="change_field(event, 1,'txt')">
<input type="text" name="txt_r1" id="txt_r1" onkeydown="change_field(event, 2,'txt')">
<input type="text" name="txt2" id="txt2" onkeydown="change_field(event, 3,'txt')">
<input type="text" name="txt_r2" id="txt_r2" onkeydown="change_field(event, 1,'txt')">
<input type="text" name="txt3" id="txt3" onkeydown="change_field(event, 2,'txt')">
<input type="text" name="txt_r3" id="txt_r3" onkeydown="change_field(event, 3,'txt')">
沒有留言:
張貼留言