如果 v-model 是数组,则检查 Vuejs 单选按钮

mtl*_*pay 3 radio-button checked vue.js v-for v-model

如果我的 v-model 是一个数组,我如何检查 v-for 中的单选按钮?

我的数据:

data() {
     return {
         persons: {
             name: [],
             surname: [],
             sex: [],
         }
     }
 }  
Run Code Online (Sandbox Code Playgroud)

还有我的收音机:

<template v-for(person, person_index) in persons>
<div class="switch-sex">
   <input type="radio" name="sex" :id="'male'+person_index" value="male"
   v-model="persons.sex[person_index]">
   <label :for="'male' + person_index">M</label>
   <input type="radio" name="sex" :id="'female' + person_index" 
    value="female" v-model="persons.sex[person_index]">
   <label :for="'female' + person_index">F</label>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)

我需要在 v-for 中的每个人中检查我的第一台收音机(男)

gil*_*gil 5

如果我没有误解你的问题和你的目标,你为多人做动态表格,那么试试这样

模板

 //n and index used for 0-based looping

 <div v-for="(n, index) in noOfPersons" :key="index">
     Person {{ index + 1 }}
     <div class="switch-sex">
        <input type="radio" :name="'sex'+(index+1)"  value="male" v-model="persons[index].sex">
        <label >Male {{index+1}}</label>
     </div>
     <div>
        <input type="radio" :name="'sex'+(index+1)" value="female" v-model="persons[index].sex">
        <label >Female {{index+1}} </label>
     </div>
 </div>
Run Code Online (Sandbox Code Playgroud)

脚本(只是一个例子来显示它的检查)

 data() {
    return {
        noOfPersons: 2,
        persons: [
            {name: '', surname: '', sex: 'male'},
            {name: '', surname: '', sex: 'female'},
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

对于那些使用 Vuetify.js 的人(包装器v-model上的方法不同v-radio-group

 <v-radio-group v-model="persons[index].sex" :mandatory="false">
     <v-radio label="Male" :value="1" color="blue"></v-radio>
     <v-radio label="Female" :value="0" color="blue"></v-radio>
 </v-radio-group>
Run Code Online (Sandbox Code Playgroud)

这是代码笔

笔记。建议使用binary (0/1)类似0formale1for 的数据female或其他数字,例如1/2 Database Storage / ISO 5218或虚拟变量。这里的解释