按角度4中的回车键提交表格

问题描述 投票:0回答:4

按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>
angular angular4-forms
4个回答
2
投票

在提交按钮中提供(keyup.enter)="yourFunction()"


0
投票

这应该工作

(keydown)="keyDownFunction($event)

在打字稿中

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

这对我有用。我想你在比赛前错过了$。


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


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/

© www.soinside.com 2019 - 2024. All rights reserved.