【问题标题】:How to handle key press to avoid deletion of input text field data如何处理按键以避免删除输入文本字段数据
【发布时间】:2018-07-13 21:05:53
【问题描述】:

我正在尝试处理按键操作以避免在按下键盘上的删除按钮时删除输入文本字段数据。

这是我的输入字段 html 代码:

<input type="time" class="custome-text-input" [(ngModel)]="d.hoursofoperationfrom"  formControlName="hoursofoperationfrom" [required]="days[i].checked" [readonly]="!days[i].checked">                         

【问题讨论】:

    标签: angular


    【解决方案1】:

    您正在寻找keydown 事件。

    按下删除键时,应在事件上调用preventDefault

    模板:

    <input type="time" class="custome-text-input" [(ngModel)]="d"  (keydown)="onKeyDown($event)">
    

    组件.ts

    public onKeyDown(event: KeyboardEvent): void {
        console.log(event.keyCode);
        if (event.keyCode === 46) {   // 46 - Delete key
          event.preventDefault();
        }
    }
    

    Stackblitz

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多