代码之家  ›  专栏  ›  技术社区  ›  Eric

使Enter键的行为与JavaScript中的Tab键类似

  •  3
  • Eric  · 技术社区  · 15 年前

    到目前为止,我一直在按键盘。

    function chkenter()
    {
      e = event.keyCode;
    
      if (e == 13)
      {
        event.keyCode = 9;
        return false;
      }
    }
    

    但即使我将keycode设置为9,它也不会切换到下一个控件。我怎样才能做到这一点?我在ASP Classic工作。

    我最终想要的是焦点转移到下一个元素。然而,下一个元素的ID并没有完全确定,因为它的ID是在循环中设置的,但此时它已经存在。 如何将焦点设置到下一个控件是个问题。

    2 回复  |  直到 12 年前
        1
  •  1
  •   Cleiton    15 年前

    这是一个纯IE的脚本,所以如果你尝试在Firefox上运行它,它将无法工作。

    要使其在IE上工作,请从代码中删除“return false”;不要忘记将此函数命名为“onkeydown”事件。

        2
  •  0
  •   vsync    12 年前

    你可以用 jQuery 像这样:

    function checkKey(e){
        if( e.keyCode == 13 ){ // if enter key
            var e = jQuery.Event('keydown'); // create a manual event
            e.which = e.charCode = 0;
            e.keyCode = 9; // set the new event to "tab"
            $(this.target).trigger(e); // trigger the new event (on the document)
            return false;  // cancels the original "enter" event from executing
        }
    }
    // lets say you bind the event on the whole document...
    $(document).on('keydown', checkKey);