如何清除某个按钮上的输入值? [英] How to clear an input value on some button press?
本文介绍了如何清除某个按钮上的输入值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在用户按下逗号(,)时清除输入。所以我要做的是在(keypress)
上检查 keyCode
,如果它是逗号,我会清除通过将输入值设置为 input.value =''
来输入。
I'm trying to clear an input when a user presses comma (,). So what I did was, on (keypress)
I would check the keyCode
and if it's a comma I would clear the input by setting the input value to 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');
}
}
推荐答案
在 preventDefault()
> clearInput 代码,如下所示:
Add preventDefault()
in your clearInput
code as shown below:
clearInput (e: KeyboardEvent) {
if (e.keyCode === 44) {
e.preventDefault(); // <-- Here
this.element.nativeElement.value = '';
} else {
console.log('Not a comma');
}
}
这篇关于如何清除某个按钮上的输入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文