Angular2 - Enter Key执行表单上的第一个(单击)功能

时间:2017-04-27 15:43:12

标签: angular angular2-forms

我不知道为什么每次我在输入元素中按ENTER键 应用程序执行它在html中找到的第一个函数。

我该如何预防?

我尝试使用(keyup.enter)事件进行一些测试,但它首先执行第一个(点击)功能,然后执行(keyup.enter)功能......

这是代码:

html表单

<form role="form" [formGroup]="FormOffertDetail" (keyup.enter)="checkKey()">

<p-dialog modal="modal" [(visible)]="displayModal_Duplicate" width="300" responsive="true">
  <p-header>
    Dupplica Offerta
  </p-header>

  <div class="form-group">
    <label for="idCustomer">Selezionare il Cliente</label>
    <!--<input formControlName="idCustomer" type="text" class="form-control input-sm" id="idCustomer" placeholder="" required autofocus>-->
    <select id="idCustomer_dup" class="form-control" formControlName="idCustomer_dup" [(ngModel)]="idCustomer_dup">
                    <option [value]="c.idCustomer" *ngFor="let c of listCustomers">
                      {{c.businessName}}
                    </option>
                    </select>
  </div>

  <p-footer>
    <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
      <button class="btn btn-primary btn-space" (click)="do_OffertDuplicate()"><i class="fa fa-copy fa-lg" aria-hidden="true"></i> Dupplica</button>
      <button class="btn btn-danger btn-space" (click)="displayModal_Duplicate=false"><i class="fa fa-trash-o fa-lg" aria-hidden="true"></i> Annulla</button>
    </div>
  </p-footer>
</p-dialog>

...

component.ts函数

 do_OffertDuplicate() {
    if (this.idCustomer_dup == null) {
      alert("Selezionare il Cliente");
      return;
    }

    this.offertService.Duplicate(0, this.offert.idOffert, this.idCustomer_dup).subscribe(
      res => {
        this.displayModal_Duplicate = false;
        alert("OFFERTA DUPPLICATA ( "+res.idOffert+" )");
        this.router.navigate(['OffertDetail', res.idOffert]);

      }
    );

  }

checkKey()
{
  alert();
}

感谢支持!

1 个答案:

答案 0 :(得分:27)

如果按钮上没有类型,则会将其视为提交按钮,因此可能会尝试提交表单。尝试将类型添加到按钮标记:

<button
  ...
  type="button"
  ...
>