简体   繁体   English

如何在选择第二个单选按钮时取消选中第一个单选按钮,并在有 2 个单选组时取消选中?

[英]How can uncheck the first radio button when selecting the second one and reverse when having 2 radio groups?

I want to archive that if i select the first radio button i should receive value true and the second radio button should be unselected and receive the value false also in reverse if i select the second radio button i should receive value true and the first radio button should be unselected and receive value false : Important is that on submit to send 2 parameters on backend True/False for each Radio that is the reason why i have not placed them in the same group我想存档如果我 select 第一个单选按钮我应该接收值true并且第二个单选按钮应该被取消选择并且如果我 select 第二个单选按钮我应该接收true false第一个单选按钮应该取消选择并接收值false :重要的是在提交时为每个 Radio 在后端发送 2 个参数 True/False 这就是我没有将它们放在同一组中的原因

This is my actual html Component:这是我的实际 html 组件:

  <div class="col-3 col-sm-3 col-md-3 mb-2">
      <mat-radio-group aria-label="Select an option" formControlName="male">
        <mat-radio-button [value]="true" [checked]="false" [(ngModel)]="male" name="male" (change)="firstRadioChange($event)">Male</mat-radio-button>
      </mat-radio-group>
      <mat-radio-group aria-label="Select an option" formControlName="female">
      <mat-radio-button [value]="false" [checked]="false" [(ngModel)]="female" name="female" (change)="secondRadioChange($event)" >Female</mat-radio-button>
    </mat-radio-group>
    </div>

This is my ts:这是我的 ts:

 firstRadioChange($event){
    console.log($event.value) 
  }

  secondRadioChange($event){
    console.log($event.value) 
  }

I would simply group the radio buttons, by default only one of them can be enabled.我只是将单选按钮分组,默认情况下只能启用其中一个。

<mat-radio-group aria-label="Select an option" (change)="onChange($event)">
  <mat-radio-button value="1">Option 1 Male</mat-radio-button>
  <mat-radio-button value="2">Option 2 Female</mat-radio-button>
  <br>
  <br>
  Selected value: {{selectedValue | json}}, send to backend sex as: {{selectedSexObj | json}}
</mat-radio-group>
@Component({
  selector: 'radio-overview-example',
  templateUrl: 'radio-overview-example.html',
  styleUrls: ['radio-overview-example.css'],
})
export class RadioOverviewExample {
  selectedSexObj = { male: false, female: false };
  selectedValue: any;
  onChange(event: any) {
    this.selectedValue = event.value;
    if (event.value == 1) {
      this.selectedSexObj = { male: true, female: false };
    } else {
      this.selectedSexObj = { male: false, female: true };
    }
  }
}

Here is a working example: https://stackblitz.com/edit/angular-qggcqg-qydurn?file=src%2Fapp%2Fradio-overview-example.ts这是一个工作示例: https://stackblitz.com/edit/angular-qggcqg-qydurn?file=src%2Fapp%2Fradio-overview-example.ts

You can follow this example你可以按照这个例子

Radio-buttons should typically be placed inside of an unless the DOM structure would make that impossible (eg, radio-buttons inside of table cells).单选按钮通常应该放置在一个内部,除非 DOM 结构无法做到这一点(例如,单选按钮在表格单元格内)。 The radio-group has a value property that reflects the currently selected radio-button inside of the group. radio-group 有一个 value 属性,它反映了组内当前选定的单选按钮。

在此处输入图像描述

https://stackblitz.com/edit/angular-tgufpa?file=src%2Fapp%2Fradio-overview-example.html https://stackblitz.com/edit/angular-tgufpa?file=src%2Fapp%2Fradio-overview-example.html

A couple of notes on your attempt.关于您的尝试的几点说明。

  • You are using both Template Driven-Forms and Reactive Forms , you should use just one on each input or else they might interfere with each other.您同时使用模板驱动表单反应式 Forms ,您应该在每个输入上只使用一个,否则它们可能会相互干扰。 In the demo below, I have used the Reactive Forms approach.在下面的演示中,我使用了反应式 Forms方法。
  • You used 2 mat-radio-group but you should use just one for the UI, then before sending the data, you modify the data according to your needs.您使用了 2 个mat-radio-group但您应该只使用一个用于 UI,然后在发送数据之前,根据您的需要修改数据。

Here is the relevante code:这是相关的代码:

HTML HTML

<div class="col-3 col-sm-3 col-md-3 mb-2">
  <mat-radio-group aria-label="Select an option" [formControl]="gender">
    <mat-radio-button value="male"> Male </mat-radio-button>
    <mat-radio-button value="female"> Female </mat-radio-button>
    <mat-radio-button value="other"> Other </mat-radio-button>
  </mat-radio-group>
</div>

TS TS

gender: FormControl = new FormControl('male');

submit() {
  const responseToServer = {
    male: this.gender.value === 'male',
    female: this.gender.value === 'female',
    other: this.gender.value === 'other',
  };

  this.http.post(responseToServer);
}

StackBlitz Demo StackBlitz 演示

Im not familiar with angular, are you submitting the form to server side in the end?我不熟悉angular,你到底是把表单提交到服务器端吗? If so you could use one radio group with both options as ui but sending it as a first options true/false data, and for second option could use hidden input which you set manually on change of radio group to opposite value of radio group.如果是这样,您可以使用两个选项作为 ui 的一个无线电组,但将其作为第一个选项 true/false 数据发送,对于第二个选项可以使用隐藏输入,您可以在将无线电组更改为无线电组的相反值时手动设置该输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在选中单选按钮时取消选中复选框,以及在使用Javascript选中复选框时如何取消选中单选按钮 - How to uncheck checkbox when radio button is checked and uncheck radio button when checkboxes are checked using Javascript 如何在选择第一个单选输入时选择所有复选框,并在选择第二个单选输入时取消选中所有复选框? (JavaScript)的 - How do I Select All Checkboxes When First Radio Input Selected & Uncheck All Checkboxes when Second radio input selected? (Javascript) 检查时取消选中其他单选按钮,有问题 - Uncheck other radio buttons when checking one, having problems 选择一个单选按钮时,Javascript单选按钮取消选择其他按钮? - Javascript radio button deselect others when selecting one radio button? 如何通过再次单击它来取消选中单选按钮 - How to uncheck a radio button by clicking on it a second time 如何在第二次单击时取消选中自定义单选按钮? - How to uncheck custom radio button on second click? 选择第二个单选按钮时未选中第一个单选按钮 - The first radio button is unchecked while selecting the second one 在IE中选择禁用的单选按钮时如何保持选中状态? - How to keep radio button checked when selecting a disabled one in IE? 第二次单击时取消选中单选按钮 - Radio button uncheck on second click 如何取消选中单选按钮 - How to Uncheck A radio button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM