Change Enter from submission to Tab?

后端 未结 3 1427
眼角桃花
眼角桃花 2020-12-03 08:25

Users don\'t like the fact that the Enter key submits the page. So I am tasked with preventing the submission and changing the Enter key to a Tab to the next field.

3条回答
  •  独厮守ぢ
    2020-12-03 08:55

    Based on this post: http://forum.jquery.com/topic/how-to-find-next-node-in-focus-order

    I came up with this. I eventually chose not to use focasables though, and instead use input to get the effect I wanted. The .not is to prevent image buttons and submit buttons from being effected, so that they still have the default action of submit on enter whenever they have focus.

    $(document).ready(function() {
    var focusables = $(":input").not('[type="image"]').not('[type="submit"]');
    
      focusables.keydown(function(e) {
        if (e.keyCode == 13) {
          e.preventDefault();
          var current = focusables.index(this),
                  next = focusables.eq(current + 1).length ? focusables.eq(current + 1) : focusables.eq(0);
          next.focus();
        }
      });
    });
    

提交回复
热议问题