<input type="tel" (keydown)="numberOnlyValidation($event)">//函数无法读取输入数值
//在输入字段中键入时,没有显示任何内容,主要无法读取键盘数值
numberOnlyValidation(event: any) {
console.log(event.target.value)
//pattern
const pattern = /[0-9]/;
const inputChar = String.fromCharCode(event.charCode);
if (!pattern.test(inputChar)) {
// invalid character, prevent input
event.preventDefault();
}
}
``发布于 2021-04-28 15:59:47
.charCode属性仅存在于keypress事件Keyboard Event (developer.mozilla.org)上
The specification says to use the KeyboardEvent.key property instead (developer.mozilla.org)
除了处理delete之外,如果您将此行更改为使用event.key,您的代码也应该可以工作
const inputChar = String.fromCharCode(event.charCode); // remove
const inputChar = event.key; // addhttps://stackoverflow.com/questions/67295452
复制相似问题