【发布时间】:2019-01-15 14:30:18
【问题描述】:
我正在尝试在用户按下逗号 (,) 时清除输入。所以我所做的是,在(keypress) 上,我会检查keyCode,如果是逗号,我会通过将输入值设置为input.value = '' 来清除输入。
HTML:
<input type="text" #elem (keypress)="clearInput($event)">
代码:
@ViewChild( 'elem' ) public element;
clearInput( e: KeyboardEvent ) {
if ( e.keyCode === 44 ) {
this.element.nativeElement.value = '';
} else {
console.log('Not a comma');
}
}
【问题讨论】:
标签: javascript html dom-events