VUE问题,有五个单选按钮组只有全选是才能提交,如果有一个选否就不可以提交,如图所示?

如题所述

第1个回答  2023-08-31
模板里:
<template>
<ul>
<li>
旅游: <input type="radio" v-model="value1" value="y" @change="getRadioVal(1)">是
<input type="radio" v-model="value1" value="n" @change="getRadioVal(1)">否
</li>
<li>
看书: <input type="radio" v-model="value2" value="y" @change="getRadioVal(2)">是
<input type="radio" v-model="value2" value="n" @change="getRadioVal(2)">否
</li>
<li>
游泳: <input type="radio" v-model="value3" value="y" @change="getRadioVal(3)">是
<input type="radio" v-model="value3" value="n" @change="getRadioVal(3)">否
</li>
<li>
跑步: <input type="radio" v-model="value4" value="y" @change="getRadioVal(4)">是
<input type="radio" v-model="value4" value="n" @change="getRadioVal(4)">否
</li>
<li>
跳高: <input type="radio" v-model="value5" value="y" @change="getRadioVal(5)">是
<input type="radio" v-model="value5" value="n" @change="getRadioVal(5)">否
</li>
</ul>
</template>
data里的定义数据:

data() {
return {
value1: 'y',
value2: 'y',
value3: 'y',
value4: 'y',
value5: 'y',
total: ['y', 'y', 'y', 'y', 'y'], //这里是每一项里选择的值,默认都是选中状态
}
},
逻辑里-methods里:

getRadioVal(num){
let val = this[`value${num}`]
this.total[num] = val
console.log(this.total)
//提交时,只要判断total里是否有“否”就可以了
if(this.total.indexOf('n') >= 0) {
console.log('不可提交')
} else {
console.log('可提交')
}
},
相似回答