我正在尝试禁用和启用单选按钮,具体取决于值。在我的代码中,如果我在文本框中输入“是”,则单击“确定”按钮单选按钮将被禁用。但是,如果我没有键入任何单选按钮2则不会禁用。我不知道发生了什么。有人可以在我的代码中找到错误吗?
https://stackblitz.com/edit/radio-button-gdpwvc?file=src%2Fapp%2Fapp.component.html
clickit(){
if( this.entervalues=="yes" ) {
this.radio1=true;
}
if( this.entervalues=="no" ) {
this.radio2=true;
}
}
答案 0 :(得分:2)
您只需要一个变量即可保存单选按钮的值
尝试使用html
<div class="form-check-inline">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="radio" [(ngModel)]="radio1" [disabled]="disabled" [value]="true">Yes
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="radio" [(ngModel)]="radio1" [disabled]="!disabled" [value]="false">No
</label>
</div>
,这在component.ts中,
clickit(){
if( this.entervalues=="yes" ) {
this.disabled=true;
}
if( this.entervalues=="no" ) {
this.disabled=false;
}
}
答案 1 :(得分:0)
您不需要2个变量:
radio;
entervalues;
clickit(){
this.radio = this.entervalues === "yes";
}
在模板中:
<label class="form-check-label">
<input type="radio" class="form-check-input" name="radio" [(ngModel)]="radio" [value]="true">Yes
</label>
...
<label class="form-check-label">
<input type="radio" class="form-check-input" name="radio" [(ngModel)]="radio" [value]="false">No
</label>
答案 2 :(得分:0)
我检查了您的代码。
基本问题是您在.ts中将radio2
的值设置为true,但在html属性中绑定了false
。因此,如果与其绑定的模态值为radio2
,则将选择false
。
link for modified code (changed radio2
to false on click if value in text box is no)