angularjs move focus to next control on enter

后端 未结 10 917
南旧
南旧 2020-11-30 07:07

What is the best way, when hitting enter inside a form, the focus to go to the next input instead submitting the form with angularjs.

I have a form with a lot of fi

10条回答
  •  清歌不尽
    2020-11-30 07:20

    I suggest making a custom directive. Something like this. I haven't tested this.

    .directive('focus', function() {
      return {
        restrict: 'A',
        link: function($scope,elem,attrs) {
    
          elem.bind('keydown', function(e) {
            var code = e.keyCode || e.which;
            if (code === 13) {
              e.preventDefault();
              elem.next().focus();
            }
          });
        }
      }
    });
    

    Something like that should work. You might have to tweek something. Good luck.

提交回复
热议问题