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

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

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

问题

以下是您的翻译部分:

  1. 我正在使用Angular 13以下是HTML代码有两个单选按钮组teamStrengthcompanyStrength我想在按钮单击时获取所选单选按钮的ID或名称以供loadDataForNextTab()方法使用
  2. <div class="section team_strength">
  3. <h3 class="team-heading">Team Strength:</h3>
  4. <div class="team-wrapper">
  5. <!-- 这里是一组单选按钮 -->
  6. </div>
  7. </div>
  8. <div class="section company_strength">
  9. <h3 class="company-heading">Company Strength:</h3>
  10. <div class="company-wrapper">
  11. <!-- 这里是另一组单选按钮 -->
  12. </div>
  13. </div>
  14. <div class="section bottom">
  15. <div class="button-wrapper">
  16. <button class="btn btn-danger butn butn-next" (click)="loadDataForNextTab()">Next</button>
  17. <button class="btn butn butn-skip">Skip for now</button>
  18. </div>
  19. </div>
  20. 目前我正在使用以下TypeScript代码来获取ID
  21. const dom: HTMLElement = this.elementRef.nativeElement;
  22. const teamStrengthelements = dom.querySelectorAll('.teamsStrength');
  23. const companyStrengthelements = dom.querySelectorAll('.companyStrength');
  24. let teamElementIdHolder: String = '';
  25. let companyElementIdHolder: String = '';
  26. teamStrengthelements.forEach((element: any) => { if (element.checked) { teamElementIdHolder = element.id } });
  27. companyStrengthelements.forEach((element: any) => { if (element.checked) { companyElementIdHolder = element.id } });
  28. 这种方法是否合适还有更好的处理方法吗请帮忙提供建议

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

英文:

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().

  1. &lt;div class=&quot;section team_strength&quot;&gt;
  2. &lt;h3 class=&quot;team-heading&quot;&gt;Team Strength:&lt;/h3&gt;
  3. &lt;div class=&quot;team-wrapper&quot;&gt;
  4. &lt;div class=&quot;input-wrapper&quot;&gt;
  5. &lt;input type=&quot;radio&quot; name=&quot;team-strength&quot; id=&quot;team-me&quot; class=&quot;teamsStrength&quot;&gt;
  6. &lt;label for=&quot;team-me&quot;&gt;Just Me&lt;/label&gt;
  7. &lt;/div&gt;
  8. &lt;div class=&quot;input-wrapper&quot;&gt;
  9. &lt;input type=&quot;radio&quot; name=&quot;team-strength&quot; id=&quot;team-one_five&quot; class=&quot;teamsStrength&quot;&gt;
  10. &lt;label for=&quot;team-one_five&quot;&gt;1 - 5&lt;/label&gt;
  11. &lt;/div&gt;
  12. &lt;div class=&quot;input-wrapper&quot;&gt;
  13. &lt;input type=&quot;radio&quot; name=&quot;team-strength&quot; id=&quot;team-five_thirty&quot; class=&quot;teamsStrength&quot; checked&gt;
  14. &lt;label for=&quot;team-five_thirty&quot;&gt;5 - 30&lt;/label&gt;
  15. &lt;/div&gt;
  16. &lt;div class=&quot;input-wrapper&quot;&gt;
  17. &lt;input type=&quot;radio&quot; name=&quot;team-strength&quot; id=&quot;team-thirty_fifty&quot; class=&quot;teamsStrength&quot;&gt;
  18. &lt;label for=&quot;team-thirty_fifty&quot;&gt;30 - 50&lt;/label&gt;
  19. &lt;/div&gt;
  20. &lt;div class=&quot;input-wrapper&quot;&gt;
  21. &lt;input type=&quot;radio&quot; name=&quot;team-strength&quot; id=&quot;team-fifty_hundread&quot; class=&quot;teamsStrength&quot;&gt;
  22. &lt;label for=&quot;team-fifty_hundread&quot;&gt;50 - 100&lt;/label&gt;
  23. &lt;/div&gt;
  24. &lt;div class=&quot;input-wrapper&quot;&gt;
  25. &lt;input type=&quot;radio&quot; name=&quot;team-strength&quot; id=&quot;team-hundread_twoHundread&quot; class=&quot;teamsStrength&quot;&gt;
  26. &lt;label for=&quot;team-hundread_twoHundread&quot;&gt;100 - 200&lt;/label&gt;
  27. &lt;/div&gt;
  28. &lt;div class=&quot;input-wrapper&quot;&gt;
  29. &lt;input type=&quot;radio&quot; name=&quot;team-strength&quot; id=&quot;team-twoHundred_fiveHundred&quot; class=&quot;teamsStrength&quot;&gt;
  30. &lt;label for=&quot;team-twoHundred_fiveHundred&quot;&gt;200 - 500&lt;/label&gt;
  31. &lt;/div&gt;
  32. &lt;div class=&quot;input-wrapper&quot;&gt;
  33. &lt;input type=&quot;radio&quot; name=&quot;team-strength&quot; id=&quot;team-fiveHundredPlus&quot; class=&quot;teamsStrength&quot;&gt;
  34. &lt;label for=&quot;team-fiveHundredPlus&quot;&gt;500+&lt;/label&gt;
  35. &lt;/div&gt;
  36. &lt;/div&gt;
  37. &lt;/div&gt;
  38. &lt;div class=&quot;section company_strength&quot;&gt;
  39. &lt;h3 class=&quot;company-heading&quot;&gt;Company Strength:&lt;/h3&gt;
  40. &lt;div class=&quot;company-wrapper&quot;&gt;
  41. &lt;div class=&quot;input-wrapper&quot;&gt;
  42. &lt;input type=&quot;radio&quot; name=&quot;company-strength&quot; id=&quot;company-one_five&quot; class=&quot;companyStrength&quot;&gt;
  43. &lt;label for=&quot;company-one_five&quot;&gt;1 - 5&lt;/label&gt;
  44. &lt;/div&gt;
  45. &lt;div class=&quot;input-wrapper&quot;&gt;
  46. &lt;input type=&quot;radio&quot; name=&quot;company-strength&quot; id=&quot;company-five_thirty&quot; class=&quot;companyStrength&quot;&gt;
  47. &lt;label for=&quot;company-five_thirty&quot;&gt;5 - 30&lt;/label&gt;
  48. &lt;/div&gt;
  49. &lt;div class=&quot;input-wrapper&quot;&gt;
  50. &lt;input type=&quot;radio&quot; name=&quot;company-strength&quot; id=&quot;company-thirty_fifty&quot; class=&quot;companyStrength&quot;&gt;
  51. &lt;label for=&quot;company-thirty_fifty&quot;&gt;30 - 50&lt;/label&gt;
  52. &lt;/div&gt;
  53. &lt;div class=&quot;input-wrapper&quot;&gt;
  54. &lt;input type=&quot;radio&quot; name=&quot;company-strength&quot; id=&quot;company-fifty_hundread&quot; class=&quot;companyStrength&quot;&gt;
  55. &lt;label for=&quot;company-fifty_hundread&quot;&gt;50 - 100&lt;/label&gt;
  56. &lt;/div&gt;
  57. &lt;div class=&quot;input-wrapper&quot;&gt;
  58. &lt;input type=&quot;radio&quot; name=&quot;company-strength&quot; id=&quot;company-hundread_twoHundread&quot; class=&quot;companyStrength&quot;&gt;
  59. &lt;label for=&quot;company-hundread_twoHundread&quot;&gt;100 - 200&lt;/label&gt;
  60. &lt;/div&gt;
  61. &lt;div class=&quot;input-wrapper&quot;&gt;
  62. &lt;input type=&quot;radio&quot; name=&quot;company-strength&quot; id=&quot;company-twoHundred_fiveHundred&quot; class=&quot;companyStrength&quot;&gt;
  63. &lt;label for=&quot;company-twoHundred_fiveHundred&quot;&gt;200 - 500&lt;/label&gt;
  64. &lt;/div&gt;
  65. &lt;div class=&quot;input-wrapper&quot;&gt;
  66. &lt;input type=&quot;radio&quot; name=&quot;company-strength&quot; id=&quot;company-fiveHundredPlus&quot; class=&quot;companyStrength&quot;&gt;
  67. &lt;label for=&quot;company-fiveHundredPlus&quot;&gt;500+&lt;/label&gt;
  68. &lt;/div&gt;
  69. &lt;/div&gt;
  70. &lt;/div&gt;
  71. &lt;div class=&quot;section bottom&quot;&gt;
  72. &lt;div class=&quot;button-wrapper&quot;&gt;
  73. &lt;button class=&quot;btn btn-danger butn butn-next&quot; (click)=&quot;loadDataForNextTab()&quot;&gt;Next&lt;/button&gt;
  74. &lt;button class=&quot;btn butn butn-skip&quot;&gt;Skip for now&lt;/button&gt;
  75. &lt;/div&gt;
  76. &lt;/div&gt;

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

  1. const dom: HTMLElement = this.elementRef.nativeElement;
  2. const teamStrengthelements = dom.querySelectorAll(&#39;.teamsStrength&#39;);
  3. const companyStrengthelements = dom.querySelectorAll(&#39;.companyStrength&#39;);
  4. let teamElementIdHolder: String = &#39;&#39;;
  5. let companyElementIdHolder: String = &#39;&#39;;
  6. teamStrengthelements.forEach((element: any) =&gt; { if (element.checked) { teamElementIdHolder = element.id } });
  7. 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:

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

In your ts file:

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

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:

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

In your ts file:

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

Hope this helps.

答案2

得分: 0

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

  1. [...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.

  1. [...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:

确定