在我的應用程式中,我有一個輸入欄位,用戶可以輸入城市代碼,當焦點移出時,服務將被呼叫以獲得城市的詳細資訊,搜索按鈕將被啟用和聚焦。當用戶輸入城市代碼并退出時,一切都按預期進行,但焦點沒有被設定到搜索按鈕。我知道被禁用的按鈕將不會收到焦點事件,但我在啟用按鈕后才設定焦點,但似乎仍然無法作業。
Ts
export class InputErrorsExample {
city = new FormControl(''/span>)。
搜索: boolean = false;
fetchCityDetails() {
setTimeout(() => {
//呼叫服務以獲取詳細資訊。
this.search = true;
let btn = document.getElementById('search') 。
if (btn) btn.focus()。
}, 1000)。)
}
HTML
<form class="example-form"/span>>
< mat-form-field class="example-full-width" appearance="fill">
<mat-label>城市</mat-label>/span>
< input type="text" matInput [formControl]="city"(focusout)="fetchCityDetails()">
</mat-form-field>/span>
< button tabindex="0" id="搜索" mat-raised-button color="primary" [disabled] = " ! search">/span>Search</button>
</form>/span>
這里是stackblitz的鏈接 https://stackblitz.com/edit/angular-ymkvhp?file=src/app/input-errors-example.html .
uj5u.com熱心網友回復:
嘗試用另一個setTimeout來延遲按鈕的焦點
。 fetchCityDetails(/span>) {
setTimeout(() => {
//呼叫服務以獲取詳細資訊。
this.search = true;
setTimeout(()=>/span>{
let btn = document.getElementById('search') 。
if (btn) btn.focus()。
})
}, 1000)。)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/320732.html
標籤:
上一篇:如何在JavaScript中訪問HTMLradio的值
下一篇:如何在Angular中安排任務
