在按钮点击时(位于独立的 div 中),如何获取收音机 ID 或名称。

huangapple go评论70阅读模式
英文:

How to get radio id or name on button click (located in isolated div)

问题

以下是您的翻译部分:

我正在使用Angular 13以下是HTML代码有两个单选按钮组teamStrength和companyStrength我想在按钮单击时获取所选单选按钮的ID或名称以供loadDataForNextTab()方法使用

<div class="section team_strength">
    <h3 class="team-heading">Team Strength:</h3>
    <div class="team-wrapper">
        <!-- 这里是一组单选按钮 -->
    </div>
</div>
<div class="section company_strength">
    <h3 class="company-heading">Company Strength:</h3>
    <div class="company-wrapper">
        <!-- 这里是另一组单选按钮 -->
    </div>
</div>
<div class="section bottom">
    <div class="button-wrapper">
        <button class="btn btn-danger butn butn-next" (click)="loadDataForNextTab()">Next</button>
        <button class="btn butn butn-skip">Skip for now</button>
    </div>
</div>

目前我正在使用以下TypeScript代码来获取ID

const dom: HTMLElement = this.elementRef.nativeElement;
const teamStrengthelements = dom.querySelectorAll('.teamsStrength');
const companyStrengthelements = dom.querySelectorAll('.companyStrength');
let teamElementIdHolder: String = '';
let companyElementIdHolder: String = '';
teamStrengthelements.forEach((element: any) => { if (element.checked) { teamElementIdHolder = element.id } });
companyStrengthelements.forEach((element: any) => { if (element.checked) { companyElementIdHolder = element.id } });

这种方法是否合适还有更好的处理方法吗请帮忙提供建议

请注意,我已经省略了不需要翻译的代码部分。如果您需要更多帮助或有其他问题,请随时提出。

英文:

I am using Angular 13 and below is the html code. There are two radio groups teamStrength and companyStrength. I want to get id or name of selected radio on button click for the method loadDataForNextTab().

 &lt;div class=&quot;section team_strength&quot;&gt;
&lt;h3 class=&quot;team-heading&quot;&gt;Team Strength:&lt;/h3&gt;
&lt;div class=&quot;team-wrapper&quot;&gt;
&lt;div class=&quot;input-wrapper&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;team-strength&quot; id=&quot;team-me&quot; class=&quot;teamsStrength&quot;&gt;
&lt;label for=&quot;team-me&quot;&gt;Just Me&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;input-wrapper&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;team-strength&quot; id=&quot;team-one_five&quot; class=&quot;teamsStrength&quot;&gt;
&lt;label for=&quot;team-one_five&quot;&gt;1 - 5&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;input-wrapper&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;team-strength&quot; id=&quot;team-five_thirty&quot; class=&quot;teamsStrength&quot; checked&gt;
&lt;label for=&quot;team-five_thirty&quot;&gt;5 - 30&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;input-wrapper&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;team-strength&quot; id=&quot;team-thirty_fifty&quot; class=&quot;teamsStrength&quot;&gt;
&lt;label for=&quot;team-thirty_fifty&quot;&gt;30 - 50&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;input-wrapper&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;team-strength&quot; id=&quot;team-fifty_hundread&quot; class=&quot;teamsStrength&quot;&gt;
&lt;label for=&quot;team-fifty_hundread&quot;&gt;50 - 100&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;input-wrapper&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;team-strength&quot; id=&quot;team-hundread_twoHundread&quot; class=&quot;teamsStrength&quot;&gt;
&lt;label for=&quot;team-hundread_twoHundread&quot;&gt;100 - 200&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;input-wrapper&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;team-strength&quot; id=&quot;team-twoHundred_fiveHundred&quot; class=&quot;teamsStrength&quot;&gt;
&lt;label for=&quot;team-twoHundred_fiveHundred&quot;&gt;200 - 500&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;input-wrapper&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;team-strength&quot; id=&quot;team-fiveHundredPlus&quot; class=&quot;teamsStrength&quot;&gt;
&lt;label for=&quot;team-fiveHundredPlus&quot;&gt;500+&lt;/label&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;section company_strength&quot;&gt;
&lt;h3 class=&quot;company-heading&quot;&gt;Company Strength:&lt;/h3&gt;
&lt;div class=&quot;company-wrapper&quot;&gt;
&lt;div class=&quot;input-wrapper&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;company-strength&quot; id=&quot;company-one_five&quot; class=&quot;companyStrength&quot;&gt;
&lt;label for=&quot;company-one_five&quot;&gt;1 - 5&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;input-wrapper&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;company-strength&quot; id=&quot;company-five_thirty&quot; class=&quot;companyStrength&quot;&gt;
&lt;label for=&quot;company-five_thirty&quot;&gt;5 - 30&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;input-wrapper&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;company-strength&quot; id=&quot;company-thirty_fifty&quot; class=&quot;companyStrength&quot;&gt;
&lt;label for=&quot;company-thirty_fifty&quot;&gt;30 - 50&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;input-wrapper&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;company-strength&quot; id=&quot;company-fifty_hundread&quot; class=&quot;companyStrength&quot;&gt;
&lt;label for=&quot;company-fifty_hundread&quot;&gt;50 - 100&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;input-wrapper&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;company-strength&quot; id=&quot;company-hundread_twoHundread&quot; class=&quot;companyStrength&quot;&gt;
&lt;label for=&quot;company-hundread_twoHundread&quot;&gt;100 - 200&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;input-wrapper&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;company-strength&quot; id=&quot;company-twoHundred_fiveHundred&quot; class=&quot;companyStrength&quot;&gt;
&lt;label for=&quot;company-twoHundred_fiveHundred&quot;&gt;200 - 500&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;input-wrapper&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;company-strength&quot; id=&quot;company-fiveHundredPlus&quot; class=&quot;companyStrength&quot;&gt;
&lt;label for=&quot;company-fiveHundredPlus&quot;&gt;500+&lt;/label&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;section bottom&quot;&gt;
&lt;div class=&quot;button-wrapper&quot;&gt;
&lt;button class=&quot;btn btn-danger butn butn-next&quot; (click)=&quot;loadDataForNextTab()&quot;&gt;Next&lt;/button&gt;
&lt;button class=&quot;btn butn butn-skip&quot;&gt;Skip for now&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;

Currently, I am using below ts code to get id.

const dom: HTMLElement = this.elementRef.nativeElement;
const teamStrengthelements = dom.querySelectorAll(&#39;.teamsStrength&#39;);
const companyStrengthelements = dom.querySelectorAll(&#39;.companyStrength&#39;);
let teamElementIdHolder: String = &#39;&#39;;
let companyElementIdHolder: String = &#39;&#39;;
teamStrengthelements.forEach((element: any) =&gt; { if (element.checked) { teamElementIdHolder = element.id } });
companyStrengthelements.forEach((element: any) =&gt; { if (element.checked) { companyElementIdHolder = element.id } });

Is the approach good? Or is there any better way to deal with this? Please help.

答案1

得分: 1

以下是您要翻译的内容:

"There is nothing wrong with the approach but there is definitely a better way to achieve the same.

I would highly recommend looking at Angular Material library. It will help you a lot with UI creation as there are ready to use components.

In your html file:

<mat-radio-group
aria-label="Select an option"
(change)="selectionChange($event)">
Option 1
Option 2

In your ts file:

selectionChange(data: MatRadioChange) {
console.log(data.value);
}

Also would recommend to create an array of your data object and have *ngFor generate the html for you. It will make your code concise and readable.

In your html file:

<mat-radio-group
aria-labelledby="example-radio-group-label"
[(ngModel)]="favoriteSeason">
<mat-radio-button class="example-radio-button" *ngFor="let season of seasons" [value]="season">
{{season}}

In your ts file:

@Component({
selector: 'radio-ng-model-example',
templateUrl: 'radio-ng-model-example.html',
styleUrls: ['radio-ng-model-example.css'],
})
export class RadioNgModelExample {
favoriteSeason: string;
seasons: string[] = ['Winter', 'Spring', 'Summer', 'Autumn'];
}

Hope this helps.

英文:

There is nothing wrong with the approach but there is definitely a better way to achieve the same.

I would highly recommend looking at Angular Material library. It will help you a lot with UI creation as there are ready to use components.

In your html file:

&lt;mat-radio-group
aria-label=&quot;Select an option&quot;
(change)=&quot;selectionChange($event)&quot;&gt;
&lt;mat-radio-button value=&quot;One&quot;&gt;Option 1&lt;/mat-radio-button&gt;
&lt;mat-radio-button value=&quot;Two&quot;&gt;Option 2&lt;/mat-radio-button&gt;
&lt;/mat-radio-group&gt;

In your ts file:

selectionChange(data: MatRadioChange) {
console.log(data.value);
}

Also would recommend to create an array of your data object and have *ngFor generate the html for you. It will make your code concise and readable.

In your html file:

&lt;mat-radio-group
aria-labelledby=&quot;example-radio-group-label&quot;
[(ngModel)]=&quot;favoriteSeason&quot;&gt;
&lt;mat-radio-button class=&quot;example-radio-button&quot; *ngFor=&quot;let season of seasons&quot; [value]=&quot;season&quot;&gt;
{{season}}
&lt;/mat-radio-button&gt;
&lt;/mat-radio-group&gt;

In your ts file:

@Component({
selector: &#39;radio-ng-model-example&#39;,
templateUrl: &#39;radio-ng-model-example.html&#39;,
styleUrls: [&#39;radio-ng-model-example.css&#39;],
})
export class RadioNgModelExample {
favoriteSeason: string;
seasons: string[] = [&#39;Winter&#39;, &#39;Spring&#39;, &#39;Summer&#39;, &#39;Autumn&#39;];
}

Hope this helps.

答案2

得分: 0

你可以使用类似以下的代码,并根据需要修改选择器,如果有其他的单选框输入项。

[...this.elementRef.nativeElement.querySelectorAll('input[type=radio]:checked')].map(el => el.id)

这些ID是从返回的 NodeList 中提取的,可以相应地使用。

英文:

you can use something like below and modify the selector if there are other radio inputs.

[...this.elementRef.nativeElement.querySelectorAll(&#39;input[type=radio]:checked&#39;)].map(el =&gt; el.id)

This ids are extracted from the returned NodeList and can be used accordingly.

huangapple
  • 本文由 发表于 2023年5月30日 13:06:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76361742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定