[英]How to submit a form by pressing Enter Key from any field in the form in Angular Material?
我想在编辑任何字段并启用更新按钮后,按 Enter 键而不单击更新按钮来提交表单,如下面的屏幕截图所示。
此更新表单是一个对话框,单击表格行上的编辑功能即可打开该对话框。
案例:现在表单在Emp Code字段上编辑并突出显示,因此单击 enter 键应提交此表单。
下面是 HTML 中更新表单的代码示例。
<mat-card-content>
<form class="example-form" [formGroup]="docForm">
<table>
<tr>
.
.
<td>
<mat-form-field appearance="outline">
<mat-label>Emp Code</mat-label>
<input matInput name="empCode" formControlName="empCd" maxLength = "4" >
</mat-form-field>
</td>
.
.
</tr>
</table>
</form>
</mat-card-content>
<button mat-raised-button style="width: 95px;" color="primary" [disabled]='docForm.invalid || !docForm.dirty (click)="update()"> Update </button>
我尝试了一些方法,但没有按预期工作。
<button mat-raised-button (keyup.enter)="!($event.target.tagName == 'TEXTAREA' || $event.target.tagName == 'BUTTON')"
style="width: 95px;" color="primary" [disabled]='docForm.invalid || !docForm.dirty' (click)="update()"> Update </button>
提前感谢您对此的任何帮助。
您应该能够绑定到ngSubmit
事件:
<form class="example-form" [formGroup]="docForm" (ngSubmit)="docForm.valid && docForm.dirty && update()">
要触发此操作,您需要在表单中某处的某个提交类型元素,但它可能是隐藏的:
<input [style.display]="'none'" type="submit">
或者,如果您愿意,您可以只绑定到keyup.enter
上的keyup.enter
事件:
<form class="example-form" [formGroup]="docForm" (keyup.enter)="docForm.valid && docForm.dirty && update()">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.