vue选择框传值可以同时传name和id
可以的,可以在选项对象中定义 name 和 id 属性,并在选中时将整个选项对象传递。例如:
<template>
<div>
<select v-model="selectedOption">
<option v-for="option in options" :value="option">{{ option.name }}</option>
</select>
<p>选中的选项:{{ selectedOption }}</p>
</div>
</template>
<script>
export default {
data() {
return {
selectedOption: null,
options: [
{ id: 1, name: '选项 1' },
{ id: 2, name: '选项 2' },
{ id: 3, name: '选项 3' }
]
}
}
}
</script>
在上面的示例中,选项对象包含了 name 和 id 属性,将整个选项对象作为 value 值绑定到 v-model 中进行传递。在选中时,selectedOption 中保存的就是整个选项对象,可以通过 selectedOption.name 和 selectedOption.id 来获取对应的值。
原文地址: https://www.cveoy.top/t/topic/buGN 著作权归作者所有。请勿转载和采集!