给文本框添加键盘事件

实现代码:var index = 0; // 初始化单选钮索引
  function addBoard(obj)
  {       
    var event = window.event;
    if(event.keyCode == 38){ // 上方向键
                 index--;
                 var lenTr = document.getElementById('subMenuTableId').rows.length; // 获取表格所有行数
                 document.getElementById('ids'+index).checked = true;
                 var count = lenTr - 1;
                 if(index < 0){
                    index = count;
                 }
              
             }
             else if(event.keyCode == 40){ // 下方向键
                 index++;
                 var lenTr = document.getElementById('subMenuTableId').rows.length;
                 document.getElementById('ids'+index).checked = true;
                 var count = lenTr - 1;
                 if(index == count){
                    index = 0;
                 }
             }
             else if(event.keyCode == 8 || event.keyCode == 46){ // 左清除键、删除键
                 index = 1;
             }
             else if(event.keyCode == 13){ // Enter选择
              var lenTr = document.getElementById('subMenuTableId').rows.length; 

                 for(var i=1; i < lenTr; i++){
                   var radioId = 'ids' + i;
                   if(document.getElementById(radioId).checked){ // 判断单选钮是否被选中
                      ock_radio(document.getElementById(radioId));
                   }
               }
             }
             else{
                   Ajax(obj); // 异步访问数据

                   index = 0; // 初始化单选索引钮
             }
  }

你可能感兴趣的:(Ajax,文本框,键盘事件,方向键)