Prevent form submission on enter key

后端 未结 15 1138
夕颜
夕颜 2020-12-05 06:24

How can I prevent the enter key from submitting the form in angular?

Is there a way to catch the 13 key and disable it or set the form as invalid unless submitting f

相关标签:
15条回答
  • 2020-12-05 06:55
    angular.element(document).ready(function () {
        angular.element(window).keydown(function () {
            if(event.keyCode == 13) {
                      event.preventDefault();
                      return false;
            }
        });
    });
    

    Try with this in angularjs controller

    0 讨论(0)
  • 2020-12-05 06:56

    so simple, doesn't need to do anything. just add this to your form tag if you are using angular +2

    <form (keydown.enter)="$event.preventDefault()" ...>
    
    0 讨论(0)
  • 2020-12-05 06:56

    I had a similar problem, I ended up taking the button out of the form. Seeing as I use ng-click and everything is binded with ng-model it doesn't really matter if it's inside the form or not.

    I realise this is bad practice but it sure as hell beats writing a custom directive to intercept keystrokes.

    0 讨论(0)
提交回复
热议问题