【问题标题】:How to enable or disable the radio button based on other radio button in angular6如何根据角度 6 中的其他单选按钮启用或禁用单选按钮
【发布时间】:2020-02-21 17:37:33
【问题描述】:

我创建了一组单选按钮。有 3 个单选按钮 Radio1、Radio2、Radio3。 Radio3 具有子单选按钮,即 RadioSub1、RadioSub2、RadioSub3。这里我想做的是

每当我选择单选按钮 Radio1 或 Radio2 时,应禁用 Radio3 的子单选按钮 RadioSub1、RadioSub2、RadioSub3。只有当我选择 Radio3 时,才应启用 RadioSub1、RadioSub2、RadioSub3。并且始终需要选择 RadioSub1。请帮忙!!!

      <div class="form-group col-md-12">
        <div class="custom-control custom-radio">

          <ul>
            <li>
              <input name="r1" id="r1" class="custom-control-input" type="radio" value="0" (change)="Change($event)">
              <label class="custom-control-label" for="r1">Radio1</label>&nbsp;
            </li>

            <li>
              <div class="mb-2">
                <input name="r1" id="r2" class="custom-control-input" type="radio" value="1" (change)="Change($event)">
              <label class="custom-control-label" for="r2">Radio2</label>
              </div>
            </li>


            <div>
            <li>
              <input name="r1" id="r3" class="custom-control-input" type="radio" value="2" (change)="Change($event)">
              <label class="custom-control-label" for="r3">Radio3</label>&nbsp;
              <ul>

                <li>
                  <input name="subRadio" id="subRadio1" class="custom-control-input" type="radio" value="4" (change)="Change($event)" [attr.disabled]="optionDisable==false || null">
                  <label class="custom-control-label" for="subRadio1">RadioSub1</label>&nbsp;
                </li>
                <div class="row">
                    <div class="col-md-4">
                   <li>
                     <input name="subRadio" id="subRadio2" class="custom-control-input" type="radio" value="5" (change)="Change($event)"  [attr.disabled]="optionDisable==false || null">
                     <label class="custom-control-label" for="subRadio2">RadioSub2</label>&nbsp;
                   </li>

                   <li>
                     <input name="subRadio" id="subRadio3" class="custom-control-input" type="radio" value="6" (change)="Change($event)"  [attr.disabled]="optionDisable==false || null">
                     <label class="custom-control-label" for="subRadio3">RadioSub3</label>&nbsp;
                   </li>
                  </div>
                 </div>

              </ul>
            </li>
            </div>

          </ul>
        </div>
      </div>

ts 文件

Change(event){
if(event.target.value===2){
this.optionDisable=true;
}
}

【问题讨论】:

    标签: radio-button angular6


    【解决方案1】:

    试试这个代码..

        <form [formGroup]="testForm" novalidate>
         <mat-radio-group (change)="getData()" formControlName="checkbox">
    <mat-radio-button  value="one" color="primary">Radio1 </mat-radio-button>
    <mat-radio-button value="two" color="primary">Radio2 </mat-radio-button>  
    <mat-radio-button value="three" color="primary" >Radio3 </mat-radio-button> 
    </mat-radio-group>
    
     <div *ngIf="flag==1">
     <mat-radio-group formControlName="check3">
     <mat-radio-button  value="sub1" color="primary">RadioSub1 </mat-radio-button>
     <mat-radio-button  value="sub2" color="primary">RadioSub2 </mat-radio-button>  
      <mat-radio-button  value="sub2" color="primary">RadioSub3 </mat-radio-button>  
    </mat-radio-group>
    

    在ts中

      export class AppComponent implements OnInit  {
    
       public testForm: FormGroup;
      flag:number=0;
      constructor(private fb: FormBuilder){}
     ngOnInit(){
        this.flag=0;
        this.testForm = this.fb.group({
      checkbox: ['One', [Validators.required]],
      check3:['sub1']
    });
    
      }
      getData()
     {
      if(this.testForm.controls['checkbox'].value=='three')
      {
      this.flag=1;
      }
      }
     } 
    

    【讨论】:

    • 感谢您的回答。在这里,您使用 *ngIf 条件作为 Radio3 的子单选按钮。但我想做的是,当条件为假时,这三个子单选按钮应该禁用。当此条件为真时,按钮应启用。我尝试过使用模板驱动的表单,并更新了我的代码。我使用 [attr.disabled] 来启用和禁用。但它仍然无法正常工作。
    • 这对你有帮助吗?试一试...getData() { if(this.testForm.controls['checkbox'].value=='three') { this.flag=1; } 其他 { this.flag=0; } }
    • 无论如何它对我有帮助...而且我已经尝试以模板驱动的形式...我得到了解决方案..谢谢您的回复
    猜你喜欢
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多