當用戶在 INPUT 中輸入逗號時,我希望逗號自動轉換為點。是否可以?
<label>Number </label>
<input type="number" maxlength="5" />
這是復制品->鏈接
uj5u.com熱心網友回復:
你可以做一些關于面具的舊SO
創建指令
@Directive({
selector: '[notdot]'
})
export class NotDotDirective {
constructor(@Optional() @Host()private control:NgControl) {}
@HostListener('input', ['$event'])
change($event) {
const item = $event.target
const value = item.value;
const pos = item.selectionStart; //get the position of the cursor
const matchValue = value.replace(/,/g, '.')
if (matchValue!=value)
{
if (this.control)
this.control.control.setValue(matchValue, { emit: false });
item.value = matchValue;
item.selectionStart = item.selectionEnd = pos; //recover the position
}
}
}
并使用喜歡
<input notdot maxlength="5"/>
注意您也可以與模板驅動的表單或 ReactiveForms 一起使用
<input notdot [(ngModel)]="variable" maxlength="5"/>
<input notdot [formControl]="control" maxlength="5"/>
你可以在這個 stackblitz中看到
uj5u.com熱心網友回復:
使用 Angular 表單控制元件會自動將其轉換為正確的數字:
https://stackblitz.com/edit/angular-ivy-uewxfs?file=src/app/app.component.html,src/app/app.component.ts
uj5u.com熱心網友回復:
<input> number 型別的元素用于讓用戶輸入一個數字。它們包括內置驗證以拒絕非數字條目。
用戶不能在 中輸入逗號INPUT,因此首先您必須將輸入型別更改為文本。在onkeyup事件之后,您可以替換如下值:
<input type="text" onkeyup="this.value=this.value.replace(/,/g, '.');"/>
https://stackblitz.com/edit/angular-ivy-hc3mn8?file=src/app/app.component.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/480870.html
標籤:有角度的
上一篇:下拉選單空-角度
