gpt4 book ai didi

html - 如何让多个单选按钮在angular2中工作?

转载 作者:搜寻专家 更新时间:2023-10-30 21:51:05 25 4
gpt4 key购买 nike

我有一个 json 数组,类似这样:

questions = 
[
{
title: Your section is?
choices: [
{
id: 1,
label: "TestA"
},
{
id=2,
label: "TestB"
}
]
},
{
title: Your major is?
choices: [
{
id=3,
label: "Medicine"
},
{
id=4,
label: "Engineering"
}
]
}
]

我的 html:

<div *ngFor="let choice of questions">
<div *ngFor="let choice of questions.choices;let i=index">
<div class="radio-group" (click)="onSelectChoice(choice.id)">
<input type="radio" class="hide" name="choiceElements" [value]="choice.id" id="choice.label" formControlName="choiceElements">
<label [class.selected]="choice.id === selctedRadio" for="{{choice.label}}" class="" >{{choice.label}}</label>
</div>
</div>
</div>

我的TS:

selctedRadio: '';
onSelectChoice(selctedChoice: any){
this.selctedRadio = selctedChoice;
}

因此,在 html 中我需要两个问题,每个问题有两个按钮,对于每个问题我应该能够选择一个单选按钮。但就我而言,我只能选择一个单选按钮,当我转到第二个问题时,所选的第一个问题将变为未选择状态。让我知道如何从每个问题中选择一个 radio 答案。

最佳答案

当且仅当它们属于同一组时,您的单选按钮必须具有相同的名称。

您必须找到一种方法来为每个组创建一个唯一的名称。

顺便说一句,这不是 Angular 特定的,它只是单选按钮在纯 HTML 中的工作方式。

但是,有关如何以 Angular 处理单选按钮的更多信息,您可以查看: Angular2 ReactiveFormsControl: how to bind radio buttons? Angular2 - Radio Button Binding

关于html - 如何让多个单选按钮在angular2中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44542591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com