通过按Enter无需提交按钮即可提交angular2提交表单
问题描述:
是否可以提交没有提交按钮的表单(通过按Enter键) 例子:
Is it possible to submit a form that does not have submit button (by pressing enter) example :
<form [ngFormModel]="xxx" (ngSubmit)="xxxx()">
<input [(ngModel)]="lxxR" ngControl="xxxxx"/>
</form
答
也许您将 keypress
或 keydown
添加到输入字段并将事件分配给点击进入时将执行提交的功能
maybe you add keypress
or keydown
to the input fields and assign the event to function that will do the submit when enter is clicked
您的模板看起来像这样
<form (keydown)="keyDownFunction($event)">
<input type="text" />
</form
您在类中的功能将如下所示
And you function inside the your class would look like this
keyDownFunction(event) {
if (event.keyCode === 13) {
alert('you just pressed the enter key');
// rest of your code
}
}