Enter key press behaves like a Tab in Javascript

后端 未结 22 1274
说谎
说谎 2020-11-27 10:52

I\'m looking to create a form where pressing the enter key causes focus to go to the \"next\" form element on the page. The solution I keep finding on the web is...

22条回答
  •  野性不改
    2020-11-27 11:33

    There are problems with all of the implementations given here. Some don't work properly with textareas and submit buttons, most don't allow you to use shift to go backwards, none of them use tabindexes if you have them, and none of them wrap around from the last to the first or the first to the last.

    To have the [enter] key act like the [tab] key but still work properly with text areas and submit buttons use the following code. In addition this code allows you to use the shift key to go backwards and the tabbing wraps around front to back and back to front.

    Source code: https://github.com/mikbe/SaneEnterKey

    CoffeeScript

    mbsd_sane_enter_key = ->
      input_types = "input, select, button, textarea"
      $("body").on "keydown", input_types, (e) ->
        enter_key = 13
        tab_key = 9
    
        if e.keyCode in [tab_key, enter_key]
          self = $(this)
    
          # some controls should just press enter when pressing enter
          if e.keyCode == enter_key and (self.prop('type') in ["submit", "textarea"])
            return true
    
          form = self.parents('form:eq(0)')
    
          # Sort by tab indexes if they exist
          tab_index = parseInt(self.attr('tabindex'))
          if tab_index
            input_array = form.find("[tabindex]").filter(':visible').sort((a,b) -> 
              parseInt($(a).attr('tabindex')) - parseInt($(b).attr('tabindex'))
            )
          else
            input_array = form.find(input_types).filter(':visible')
    
          # reverse the direction if using shift
          move_direction = if e.shiftKey then -1 else 1
          new_index = input_array.index(this) + move_direction
    
          # wrap around the controls
          if new_index == input_array.length
            new_index = 0
          else if new_index == -1
            new_index = input_array.length - 1
    
          move_to = input_array.eq(new_index)
          move_to.focus()
          move_to.select()
    
          false
    
    $(window).on 'ready page:load', ->
      mbsd_sane_enter_key()
    

    JavaScript

    var mbsd_sane_enter_key = function() {
      var input_types;
      input_types = "input, select, button, textarea";
    
      return $("body").on("keydown", input_types, function(e) {
        var enter_key, form, input_array, move_direction, move_to, new_index, self, tab_index, tab_key;
        enter_key = 13;
        tab_key = 9;
    
        if (e.keyCode === tab_key || e.keyCode === enter_key) {
          self = $(this);
    
          // some controls should react as designed when pressing enter
          if (e.keyCode === enter_key && (self.prop('type') === "submit" || self.prop('type') === "textarea")) {
            return true;
          }
    
          form = self.parents('form:eq(0)');
    
          // Sort by tab indexes if they exist
          tab_index = parseInt(self.attr('tabindex'));
          if (tab_index) {
            input_array = form.find("[tabindex]").filter(':visible').sort(function(a, b) {
              return parseInt($(a).attr('tabindex')) - parseInt($(b).attr('tabindex'));
            });
          } else {
            input_array = form.find(input_types).filter(':visible');
          }
    
          // reverse the direction if using shift
          move_direction = e.shiftKey ? -1 : 1;
          new_index = input_array.index(this) + move_direction;
    
          // wrap around the controls
          if (new_index === input_array.length) {
            new_index = 0;
          } else if (new_index === -1) {
            new_index = input_array.length - 1;
          }
    
          move_to = input_array.eq(new_index);
          move_to.focus();
          move_to.select();
          return false;
        }
      });
    };
    
    $(window).on('ready page:load', function() {
      mbsd_sane_enter_key();
    }
    

提交回复
热议问题