简体   繁体   English

使用选择选项从JSON动态设置单选按钮

[英]Dynamically set Radio button from JSON using select option

I want to set questions from an pre-select question using Vue.js. 我想使用Vue.js从预选问题中设置问题。

I was able to get this working with an Radio button that once is checked it shows new set of questions. 我能够通过单选按钮使它工作,一旦选中该按钮,它将显示新的问题集。

I´m trying to the same using drop-down selection, once value is set on the dropdown, will show the set of Radio button questions 我正在尝试使用下拉选择进行同样的操作,一旦在下拉菜单上设置了值,就会显示单选按钮问题集

This is my current test: 这是我目前的测试:

<div id="app">
 <select v-model="selectedL">
   <option v-for="l in list" v-bind:value="{ id: l.id, text: l.name }">{{ l.name }}
   </option>
 </select>
 <div v-for="r in list.text" :key="r.id">
  <input type="radio" :value="r" v-model="selectedR" :id="r.id" :name="r.id">
  <label class="label" v-bind:for="r.id">{{r.name}}</label>
 </div>
</div>

var app = new Vue({
el: '#app',
data: {
selectedL: '',
selectedR: '',

list: [
    {
  id: 1,
  name: 'A',
  text:[
    {
      text1: '123',
      text2: '456'
    }
   ]
  },
  {
  id: 2, 
  name: 'B',
  text:[
    {
      text1: '678',
      text2: '908'
    }
   ]
  },
  {
  id: 3, 
  name: 'C',
  text:[
    {
      text1: '143',
      text2: '786'
    }
    ]
  }
]
}
})

Above is my working progress with Radio/Radio 以上是我在电台/电台的工作进度

https://jsfiddle.net/bernlt/pvndg8jf/11/ https://jsfiddle.net/bernlt/pvndg8jf/11/

I need help to do the same using an Select-option to define Radio questions 我需要帮助,使用“选择”选项来定义收音机问题

Something like this should work: 这样的事情应该起作用:

 var app = new Vue({ el: '#app', data: { selectedL: '', selectedR: '', list: [{ id: 1, name: 'A', text:[{ id: 123, question: '123', }, { id: 456, question: '456', }] }, { id: 2, name: 'B', text:[{ id: 678, question: '678', }, { id: 908, question: '908', }] }, { id: 3, name: 'C', text:[{ id: 143, question: '143', }, { id: 786, question: '786', }] }] } }) 
 <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script> <div id="app"> <select v-model="selectedL"> <option v-for="l in list" v-bind:value="l">{{ l.name }} </option> </select> <p>Questions:</p> <div v-for="(r, index) in selectedL.text" :key="r.id"> <input type="radio" :value="r" v-model="selectedR" :id="r.id" :name="r.id"> <label class="label" v-bind:for="r.id">{{r.question}}</label> </div> <p>selected Id from select: {{selectedL.id}} </p> <p> selected Id from radio: {{selectedR.id}} </p> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM