[英]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.关于您的尝试的几点说明。
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.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);
}
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.