如果输入为空,则禁用按钮 [英] disable the button if the input is empty
本文介绍了如果输入为空,则禁用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果输入为空,如何禁用按钮,并在用户开始输入输入时启用它,我尝试过:
How can I disable the button if the input is empty, and enable it when the user start typing in the input, i tried that:
<input #new_field type="text" autocomplete="off" />
<p-button (onClick)="saveNewField(new_field.value)" [disabled]="new_field.value ==''" label="Save"></p-button>
还有这个:
<input [(ngModel)]="searchText" size="30" autocomplete="off" />
<p-button (onClick)="saveNewField(searchText)" [disabled]="!searchText" label="Save"></p-button>
推荐答案
使用ngModel并绑定值并用于禁用按钮.试试这个:
Use ngModel and bind a value and use to disable button. Try this:
<input type="text" autocomplete="off" [(ngModel)]="searchText"/>
<p-button (onClick)="saveNewField(searchText)" [disabled]="!searchText" label="Save"></p-button>
打字稿文件:
export class AppComponent {
searchText: string;
saveNewField(searchText) {
console.log("searched Text", searchText);
}
}
这篇关于如果输入为空,则禁用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文