如何将每个用户输入的“0”替换为“1”
How can I replace every user input of '0' to '1'
这是我的 OnlyNumber 指令,它只适用于数字。我希望每当用户在输入字段中输入 0 时,它都会转换为 1
import { Directive, ElementRef, HostListener, Input } from '@angular/core';
@Directive({
selector: '[OnlyNumber]'
})
export class OnlyNumber {
constructor(private el: ElementRef) { }
@Input() OnlyNumber: boolean;
@HostListener('keydown', ['$event']) onKeyDown(event) {
let e = <KeyboardEvent> event;
console.log(e);
if (this.OnlyNumber) {
if ([46, 8, 9, 27, 13, 110, 190].indexOf(e.keyCode) !== -1 ||
// Allow: Ctrl+A
(e.keyCode == 65 && e.ctrlKey === true) ||
// Allow: Ctrl+C
(e.keyCode == 67 && e.ctrlKey === true) ||
// Allow: Ctrl+X
(e.keyCode == 88 && e.ctrlKey === true) ||
// Allow: Numpad Number
(e.keyCode >= 96 && e.keyCode <= 105) ||
// Allow: home, end, left, right
(e.keyCode >= 35 && e.keyCode <= 39)) {
// let it happen, don't do anything
return;
}
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
}
}
}
这是我的 html 输入字段:
<input OnlyNumber='true' MaxNumber="9" type="text" [(ngModel)]='cart.selectedQty'
(ngModelChange)='calculateTotalPrice($event,cart,i)' class="mb-0" />
请给我任何解决方案。
您可以在每个 keydown
事件之后简单地将每个“0”替换为“1”,对吗?
喜欢:
@HostListener('keyup', ['$event']) onKeyUp(event) {
this.el.nativeElement.value= this.el.nativeElement.value.replace('0', '1');
}
演示供您参考:https://stackblitz.com/edit/angular-fndxsq
我的 keyup
来源:How can I get jquery .val() AFTER keypress event?
有关replace
的更多信息:https://www.w3schools.com/jsref/jsref_replace.asp
这是我的 OnlyNumber 指令,它只适用于数字。我希望每当用户在输入字段中输入 0 时,它都会转换为 1
import { Directive, ElementRef, HostListener, Input } from '@angular/core';
@Directive({
selector: '[OnlyNumber]'
})
export class OnlyNumber {
constructor(private el: ElementRef) { }
@Input() OnlyNumber: boolean;
@HostListener('keydown', ['$event']) onKeyDown(event) {
let e = <KeyboardEvent> event;
console.log(e);
if (this.OnlyNumber) {
if ([46, 8, 9, 27, 13, 110, 190].indexOf(e.keyCode) !== -1 ||
// Allow: Ctrl+A
(e.keyCode == 65 && e.ctrlKey === true) ||
// Allow: Ctrl+C
(e.keyCode == 67 && e.ctrlKey === true) ||
// Allow: Ctrl+X
(e.keyCode == 88 && e.ctrlKey === true) ||
// Allow: Numpad Number
(e.keyCode >= 96 && e.keyCode <= 105) ||
// Allow: home, end, left, right
(e.keyCode >= 35 && e.keyCode <= 39)) {
// let it happen, don't do anything
return;
}
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
}
}
}
这是我的 html 输入字段:
<input OnlyNumber='true' MaxNumber="9" type="text" [(ngModel)]='cart.selectedQty'
(ngModelChange)='calculateTotalPrice($event,cart,i)' class="mb-0" />
请给我任何解决方案。
您可以在每个 keydown
事件之后简单地将每个“0”替换为“1”,对吗?
喜欢:
@HostListener('keyup', ['$event']) onKeyUp(event) {
this.el.nativeElement.value= this.el.nativeElement.value.replace('0', '1');
}
演示供您参考:https://stackblitz.com/edit/angular-fndxsq
我的 keyup
来源:How can I get jquery .val() AFTER keypress event?
有关replace
的更多信息:https://www.w3schools.com/jsref/jsref_replace.asp