通过按角度4中的Enter键提交表单

时间:2019-03-25 08:46:38

标签: angular angular4-forms

通过按Enter键,需要提交角度为4的表格 以下是我的Action = ""格式的代码无法正常工作。 我还尝试了(keydown) = domonething(event)(keydown.enter) = domonething(event) 使用以下代码

keyDownFunction(event) {
    if (event.keyCode == 13) {
        this.submit();
    }
}

下面是我当前的代码

<form #f="ngForm" (ngSubmit)="f.form.valid && openModal(confirmationmodal)" novalidate action="">
    <div class="form-group form-row">
        <label class="col-form-label col-sm-4 col-md-4" for="name">Employee Name</label>
        <div class="col-sm-8 col-md-8">
            <span type="text" readonly class="form-control-plaintext" id="name">{{employeeDetails.EmployeeName}}</span>
        </div>
    </div>
    <div class="form-group form-row">
        <label class="col-form-label col-sm-4 col-md-4 " for="name">Manager Name</label>
        <div class="col-md-8  col-sm-8">
            <span type="text" readonly class="form-control-plaintext"
                id="manager">{{employeeDetails.ManagerName}}</span>
        </div>
    </div>
    <div class="form-group form-row">
        <label for="name" class="col-sm-4 col-md-4 col-form-label">Subject</label>
        <div class="col-md-8 col-sm-8">
            <span type="text" readonly class="form-control-plaintext" id="manager">{{subject}}</span>
        </div>
    </div>

    <button class="btn btn-success float-right" type="submit" id="submit">Submit</button>
</form>

4 个答案:

答案 0 :(得分:2)

在您的提交按钮中给(keyup.enter)="yourFunction()"

答案 1 :(得分:0)

这应该有效

(keydown)="keyDownFunction($event)

在打字稿中

  keyDownFunction(event) {
    if ( event.keyCode === 13) {
      // do your function
    }
  }

这对我有用。我认为您在活动开始前错过了$。

答案 2 :(得分:0)

如果您想使用按钮来调用它,则可以这样做-

<button type="button" (keyup.enter)="doSomething()" >Click Me!</button>

在此处直播- https://stackblitz.com/edit/angular-3gf6hw

在此了解更多信息- https://angular.io/guide/user-input#key-event-filtering-with-keyenter

答案 3 :(得分:0)

从您的表单标签中,我将删除action属性,并添加以下内容:

<form #f="ngForm" (ngSubmit)="f.form.valid && openModal(confirmationmodal)" novalidate (keydown.enter)="onEnterKeyDown($event)">

然后只需编写按键事件的功能:

onEnterKeyDown($event) {
  // here you can open your confirmation modal if the form is valid
}

来源:https://alligator.io/angular/binding-keyup-keydown-events/