I am using Angular and TypeScript, and trying to enable/disable a textbox using a checkbox.
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")
}
}
Try to avoid modifying DOM directly with document.getElementById
in Angular. You could achieve the same effect with other options.
ngModel
directiveController
export class AppComponent {
checkboxStatus: any;
...
}
Template
<input type="checkbox" [(ngModel)]="checkboxStatus" (click)="check_en(value)">
<input type="text" id="text1" [disabled]="!checkboxStatus">
Here we are two-way binding the value of the checkbox to the variable checkboxStatus
using the ngModel
directive.
Template
<input #inputCheck type="checkbox" (click)="check_en(value)">
<input type="text" id="text1" [disabled]="!inputCheck.checked">
Here a template reference variable inputCheck
is used to refer to the checkbox within the DOM. Later it's property checked
is bound to the text input property disabled
to dynamically set it's value.
to set checkbox
checked, you have to checked=true
and not by value
.
For Example:
<input type="checkbox" checked=true (click)="check_en(value)">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.