如何在内容可编辑表中捕获键盘事件?

时间:2019-11-28 14:21:57

标签: javascript jquery html css

比方说,我有一个HTML表:

  • 包含可编辑的单元格
  • rowspans

在内容可编辑的单元格中,可以按键盘上的“ Enter”键,如果我只允许用户输入单行数据,则没有任何意义。

我想要的是,当用户在可编辑单元格上按Enter键时,它应聚焦到最近的可编辑单元格(向右/向下)以进行新输入。

我应该如何解决问题?

<table>
   <tr>
      <th>Data 1</th>
      <th>Data 2</th>
      <th>Data 3</th>
      <th>Data 4</th>
      <th>Data 5</th>
      <th>Data 6</th>
   </tr>
   <tr>
      <td rowspan="3" contenteditable="true"><label><input type="radio" name="typeOf0" checked=""> <span>Radio 1</span> </label><br><label><input type="radio" name="typeOf0"> <span>Radio 2</span> </label><br></td>
      <td rowspan="3" contenteditable="true">0</td>
      <td contenteditable="true">0</td>
      <td contenteditable="true">0</td>
      <td contenteditable="true">0</td>
      <td rowspan="3" contenteditable="true">0</td>
   </tr>
   <tr>
      <td contenteditable="true">0</td>
      <td contenteditable="true">0</td>
      <td contenteditable="true">0</td>
   </tr>
   <tr>
      <td contenteditable="true">0</td>
      <td contenteditable="true">0</td>
      <td contenteditable="true">0</td>
   </tr>
   <tr></tr>
   <tr>
</table>

1 个答案:

答案 0 :(得分:1)

诀窍是捕捉按键信息并处理回车(键代码13)。

您可以编辑@filemono中已经提供的示例,以使其可以与enter一起使用。

(function ($) {

  $.fn.enableCellNavigation = function () {

    var keys = {
      left: 37,
      up: 38,
      right: 39,
      down: 40,
      enter: 13
    };

    // select all on focus
    // works for input elements, and will put focus into
    // adjacent input or textarea. once in a textarea,
    // however, it will not attempt to break out because
    // that just seems too messy imho.
    this.find('input').keydown(function (e) {

      // shortcut for key other than arrow keys
      if ($.inArray(e.which, [keys.left, keys.up, keys.right, keys.down, keys.enter]) < 0) {
        return;
      }

      var input = e.target;
      var td = $(e.target).closest('td');
      var moveTo = null;

      switch (e.which) {

        case keys.left:
          {
            if (input.selectionStart == 0) {
              moveTo = td.prev('td:has(input,textarea)');
            }
            break;
          }
        case keys.right:
          {
            if (input.selectionEnd == input.value.length) {
              moveTo = td.next('td:has(input,textarea)');
            }
            break;
          }

        case keys.up:
        case keys.enter:
        case keys.down:
          {

            var tr = td.closest('tr');
            var pos = td[0].cellIndex;

            var moveToRow = null;
            if (e.which == keys.down || e.which == keys.enter) {
              moveToRow = tr.next('tr');
            } else if (e.which == keys.up) {
              moveToRow = tr.prev('tr');
            }

            if (moveToRow.length) {
              moveTo = $(moveToRow[0].cells[pos]);
            }

            break;
          }

      }

      if (moveTo && moveTo.length) {

        e.preventDefault();

        moveTo.find('input,textarea').each(function (i, input) {
          input.focus();
          input.select();
        });

      }

    });

  };

})(jQuery);


// use the plugin
$(function () {
  $('#people').enableCellNavigation();
});