使用箭头键浏览表单

时间:2017-02-07 16:28:30

标签: html forms usability

我有一个html表单,它就像一个表(包含行和列)一样创建。基本上它看起来像excel。 现在我想用箭头键在excel中导航。这可能吗?

<table>
  <tr>
      <td/>
      <td/>
  </tr>      
  <tr>
      <td/>
      <td/>
  </tr>
</table>

1 个答案:

答案 0 :(得分:0)

是的,一旦我得到你的提示,这很容易。这是我的解决方案:

   var UP = 38;
    var DOWN = 40;
    var LEFT = 37;
    var RIGHT = 39;
    var TAB = 9;
    $('#tab-columns').on('keydown', 'input', function (event) {

        var $focused = $(':focus');
        var id = ($focused.parents("td").attr('id'));
        if (id.startsWith("field_")) {              
            idArr = id.replace("field_", "").split("_");
            row = Number(idArr[0]);
            col = Number(idArr[1]);

            newId = id;
            switch (event.which) {          
            case UP:
                newId = "field_"+(row-1)+"_"+col;
                break;
            case DOWN:
                newId = "field_"+(row+1)+"_"+col;
                break;
            case TAB:
            case RIGHT:
                newId = "field_"+row+"_"+(col+1);
                break;                  
            case LEFT:
                newId = "field_"+row+"_"+(col-1);
                break;
            default:
                // nothing
            }

            $("#"+newId).find("input").focus();
        }