【问题标题】:select option change don't save the value in radio input选择选项更改不要将值保存在无线电输入中
【发布时间】:2021-07-23 05:01:27
【问题描述】:

我有一个元素列表,通过radio 输入选择,必须输出[value]。问题是当我去更改选择时,选择值的select 没有找到我的次要值(当我更改时,始终保持prezzoRiepilogo 的值而不是prezzoRiepilogoTwo)。为什么?我该如何解决?

<ul class="list-group">
               <li class="list-group-item" *ngFor="let interventoStrutturale of interventi">
                   <td style="float: left;">{{interventoStrutturale.intervento.codice}} {{interventoStrutturale.variante}} - {{interventoStrutturale.intervento.descrizione}}<br>

                       <select [(ngModel)]="interventoStrutturale.tipo_sup">
                           <option disabled value="1">Selection one</option>
                           <option value="2">Selection two</option>
                           <option value="3">Selection three</option>
                       </select>
</td>

                   <td style="float: right;" [ngSwitch]="interventoStrutturale.tipo_sup">

                       <label class="form-check-inline" *ngSwitchCase="interventoStrutturale.tipo_sup == 2">
                       <input class="form-check-input" type="radio" name="radio"
                           [value]="interventoStrutturale.prezzoRiepilogo" (change)="onChangeIntervento($event.target.value)">
                     </label>

<label class="form-check-inline" *ngSwitchCase="interventoStrutturale.tipo_sup == 3">
                       <input class="form-check-input" type="radio" name="radio"
                           [value]="interventoStrutturale.prezzoRiepilogoTwo" (change)="onChangeIntervento($event.target.value)">
                     </label>

                     <label class="form-check-inline" *ngSwitchDefault>
                       <input class="form-check-input" type="radio" name="radio"
                           [value]="interventoStrutturale.prezzoRiepilogo" (change)="onChangeIntervento($event.target.value)">
                     </label>
                   </td>
               </li>
           </ul>  
           

onChangeIntervento(intervento: number) {
   this.prezzoStrutturale = intervento
   this.elementoSelezionato = []
   this.interventi.forEach(c => {
     if(this.prezzoStrutturale == c.prezzoRiepilogo) {
       this.elementoSelezionato.push(c)
     } 
     if(this.prezzoStrutturale == c.prezzoRiepilogoTwo) {
       this.elementoSelezionato.push(c)
     }
   })
}

【问题讨论】:

  • 请更详细地描述您对代码的期望。为什么总是只显示一个单选按钮?
  • 请善待并展示您的onChangeIntervento()功能。
  • @Lynx242 上面写的代码就是一个例子。选择仅在某些情况下存在。有些元素我不必从选择中选择任何东西,事实上它直接通过收音机中的*ngSwitchDefault 找到值。我必须解决我有一个选择和两个 [值] 变化的选项的情况
  • @Lynx242 好的,我已经用onChangeIntervento()编辑了帖子

标签: javascript html arrays angular typescript


【解决方案1】:

我认为您没有正确应用 SwitchCase。试试这个方法:

<td style="float: right;" [ngSwitch]="interventoStrutturale.tipo_sup">

    <label class="form-check-inline" *ngSwitchCase="2">
        <input class="form-check-input" type="radio" name="radio"
                       [value]="interventoStrutturale.prezzoRiepilogo" (change)="onChangeIntervento($event.target.value)">
    </label>

    <label class="form-check-inline" *ngSwitchCase="3">
        <input class="form-check-input" type="radio" name="radio"
                       [value]="interventoStrutturale.prezzoRiepilogoTwo" (change)="onChangeIntervento($event.target.value)">
    </label>

    <label class="form-check-inline" *ngSwitchDefault>
        <input class="form-check-input" type="radio" name="radio"
                       [value]="interventoStrutturale.prezzoRiepilogo" (change)="onChangeIntervento($event.target.value)">
    </label>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-18
    • 2011-07-27
    • 2014-04-23
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    相关资源
    最近更新 更多