首页 > 解决方案 > Angular 5按钮在输入键按下时提交

问题描述

我有一个使用所有常用模型的 Angular 5 表单应用程序,但在表单上我希望表单提交而无需实际单击“提交”按钮。

我知道这通常就像添加type=submit到我的按钮元素一样简单,但它似乎根本不起作用。

我真的不想为了onclick让它工作而调用一个函数。谁能建议我可能遗漏的任何东西。

<form (submit)="search(ref, id, forename, surname, postcode)" action="#">
  <mat-card>
    <mat-card-title class="firstCard">Investor/Adviser search</mat-card-title>
    <mat-card-content>
      <p *ngIf="this.noCriteria" class="errorMessage">Please enter search criteria.</p>
      <p *ngIf="this.notFound" class="errorMessage">No investor's or adviser's found.</p>
        <mat-form-field>
          <input matInput id="invReference" placeholder="Investor/Adviser reference (e.g. SCC/AJBS)" #ref>
        </mat-form-field>
        <mat-form-field>
          <input matInput id="invId" placeholder="Investor/Adviser ID" type="number" #id>
        </mat-form-field>
        <mat-form-field>
          <input matInput id="invForname" placeholder="Forename" #forename>
        </mat-form-field>
        <mat-form-field>
          <input matInput id="invSurname" placeholder="Surname" #surname>
        </mat-form-field>
        <mat-form-field>
          <input matInput id="invPostcode" placeholder="Postcode" #postcode>
        </mat-form-field>
    </mat-card-content>
    <mat-card-footer>
      <button mat-raised-button type="submit" class="successButton" id="invSearch" title="Click to perform search.">Search</button>
    </mat-card-footer>
  </mat-card>
</form>

标签: htmlangulartypescriptangular5

解决方案


尝试使用keyup.enterkeydown.enter

  <button type="submit" (keyup.enter)="search(...)">Search</button>

推荐阅读