方法一:数组循环遍历匹配value值相等
通过对el-option绑定的数组循环遍历匹配value值相等时获取label
缺点:如果el-option绑定的数组可动态变化就糟糕了。
方法二:value上绑定对象 不是绑定值
缺点:value绑定对象对赋值处理增加了难度
注意:官方提供了属性value-key绑定对象的唯一标识为键名,若不需要返显选中值时,推荐使用。
方法三:通过ref获取
<el-select ref="labelSelect" @change="changeSelect">
<el-option v-for="item in testArr" :key="item.name" :label="item.des" :value="item.name"></el-option>
</el-select>
changeSelect (val) {
this.$nextTick(() => {
let arr = this.$refs.labelSelect.selected
for (let i = 0; i < arr.length; i++) {
console.log(arr[i].label, i)
console.log(arr[i].currentValue, i)
console.log(arr[i].currentLabel, i)
}
})
},
注意:获取选中元素时,一定要用this.$nextTick(),不然你试试。
衍生
循环遍历时,@change事件想传递当前对象的方法:
<el-row v-for="result in resultList" :key="result.id">
<el-radio-group v-model="result.status">
<el-radio v-for="item in status" :key="item.value" :label="item.value" @change="(value) => changeStatus(result.id, value)">{{ item.label }}</el-radio>
</el-radio-group>
</el-row>