我有簡單的輸入不能得到輸入離子值3
<input type="number" maxlength=1 id="1"/>
和簡單的JS在我的.ts文件
console.log(document.getElementById("1").value);
,但我得到一個錯誤
Property 'value' does not exist on type 'HTMLElement'
我有簡單的輸入不能得到輸入離子值3
<input type="number" maxlength=1 id="1"/>
和簡單的JS在我的.ts文件
console.log(document.getElementById("1").value);
,但我得到一個錯誤
Property 'value' does not exist on type 'HTMLElement'
那麼首先你應該避免直接角,隨後在離子,這是不好的性能,使你的代碼少維護訪問DOM的。
相反,你添加一個ngModel
您輸入元素:
<ion-input [(ngModel)]="myInput"></ion-input>
和實例變量添加到您的.ts
文件:
public myInput;
你ngModel
變量將有你的輸入字段的值。您可以通過例如添加ionChange
事件偵聽器添加到輸入的更改記錄到它:
<ion-input [(ngModel)]="myInput" (ionInput)="logChange($event)"></ion-input>
而在你.ts
文件中添加的方法吧:
logChange(event) {
console.log(event);
}
您需要投射到HTMLInputElement(HtmlInputElement)。這是因爲常規HTMLElements沒有值屬性。
console.log(<HTMLInputElement>document.getElementById("1").value);
P.S.鑑於您使用的是Ionic 3,我假設您也使用Angular,並且有更好的方法來抓取dom元素,例如ViewChild。
由於您沒有給出value
屬性輸入number
,您將得到未定義的屬性。
要麼在元素中定義屬性,要麼在事件發生時將其提取出來。
<input type="number" maxlength=1 id="1" />
<button onclick="bclick()">Click</button>
<script>
function bclick(){
console.log(document.getElementById("1").value);
}
</script>
我知道這段代碼在瀏覽器中工作,但在我的離子應用程序中它不工作(。代碼正在調用按鈕提交。 – ItsMyLife
它不建議設置編號。 –
@MohamedAbbas它只是例如 – ItsMyLife
它的錯誤,只使用科爾多瓦,因爲因爲你不想使用角 –