Replace keyCode in IE 11

后端 未结 4 1018
执笔经年
执笔经年 2021-01-18 20:37

We use this code for simulating Tab key with Enter key:

function EnterTab() {
    if (event.keyCode == 13) event.keyCode = 9;
    retur         


        
4条回答
  •  不要未来只要你来
    2021-01-18 20:42

    We use this code for simulating Tab key with Enter key:
    ...
    When I hit Enter I want focus go to next control.

    The idea is wrong. Don't change enter to tab. Tackle this problem by intercepting the enter key, set the focus on next element and cancel the event. jQuery solution:

    $(function() {
      $("form").on("keypress", "input[type=text], select, textarea", function(e) {
        if (e.which === 13) {
          e.preventDefault();
          var $fields = $(this).closest("form").find(":input");
          var index = $fields.index(this);
          $fields.eq(index + 1).trigger("focus");
        }
      });
    });
    input,
    select,
    textarea {
      box-sizing: border-box;
      margin: .5em 0;
      width: 100%;
    }
    
    
    

    Hitting enter inside the text fields will not submit the form

    jsFiddle for testing in Internet Explorer

提交回复
热议问题