我正在使用 Angular 和 TypeScript,并尝试使用复选框启用/禁用文本框。
app.component.html
<input type="checkbox" value="true" (click)="check_en(value)">
<input type="text" id="text1" disabled>
app.component.ts
check_en(v1: any){
if(v1 == true){
document.getElementById('text1').setAttribute("disabled", "false");
} else {
document.getElementById('text1').removeAttribute("disabled")
}
}
尽量避免直接修改 DOMdocument.getElementById
在角度。您可以使用其他选项达到相同的效果。
选项 1:双向绑定ngModel
指示
控制器
export class AppComponent {
checkboxStatus: any;
...
}
Template
<input type="checkbox" [(ngModel)]="checkboxStatus" (click)="check_en(value)">
<input type="text" id="text1" [disabled]="!checkboxStatus">
我们到了双向绑定变量的复选框值checkboxStatus
使用ngModel指示。
选项 2:模板引用变量
Template
<input #inputCheck type="checkbox" (click)="check_en(value)">
<input type="text" id="text1" [disabled]="!inputCheck.checked">
Here a 模板引用变量 inputCheck
用于引用 DOM 中的复选框。后来是财产checked
绑定到文本输入属性disabled
动态设置它的值。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)