我有一個要求,我必須保持按鈕最初被禁用,只有當我的輸入長度為 10 時才啟用它。
<div class="form-group">
<label for="MSISDN_Value" class="m-r-10">MSISDN:</label>
<input type="number"
onkeyPress="if(this.value.length === 10) return false;"
[(ngModel)]="MSISDN_Value"
id="MSISDN_Value"
name="MSISDN_Value"
class="form-control display-inline-block width70p m-b-5">
</div>
<div class="form-group">
<button class="btn btn-primary m-l-65" type="submit"
[disabled]="true">Search</button>
</div>
我試過[disabled] = "MSISDN_Value.length !== 10" 了但沒有用,有很多禁用按鈕的解決方案,但我找不到任何在 Angular5 中啟用按鈕的解決方案。
uj5u.com熱心網友回復:
您好,問題是您正在使用回傳 false 的 === 運算子檢查整數值。看我下面的代碼。
<div class="form-group">
<label for="MSISDN_Value" class="m-r-10">MSISDN:</label>
<input type="number"
#num
max=10
(change)="toggle2($event)"
id="MSISDN_Value"
name="MSISDN_Value"
class="form-control display-inline-block width70p m-b-5">
</div>
<div class="form-group">
<button class="btn btn-primary m-l-65" type="submit"
[disabled]="this.num.value==10?false:true">Search{{this.num.value===10}}</button>
</div>
//.ts file
toggle2(event){
console.log(event.target.value)
}
uj5u.com熱心網友回復:
搜索后,我借助事件并使用(輸入)函式系結并在 even.target.value 更改上觸發事件以切換 [isDisabled]
<div class="form-group">
<label for="MSISDN_Value" class="m-r-10">ISMSDN/IMEI:</label>
<input type="number" onkeyPress="if(this.value.length === 10) return false;" (input)="onSearchChanges($event.target.value)" [(ngModel)]="MSISDN_Value" id="MSISDN_Value" name="MSISDN_Value" class="form-control display-inline-block width59p m-b-5">
</div>
<div class="form-group">
<button class="btn btn-primary m-l-96" type="submit" [disabled]="searchDisabled">Search</button>
</div>
component.ts 檔案
onSearchChanges(value) {
if(value.length === 10){
this.searchDisabled = false;
} else{
this.searchDisabled = true;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/536707.html
上一篇:僅在樣式中輸入鍵的WPF按下狀態
下一篇:爺不伺候了
