Vue Element如何獲取select選擇框選擇的value和label
1 使用watch監(jiān)聽selectedValue的變化
可以使用Element UI中的v-model指令,將選中的值和對(duì)應(yīng)的標(biāo)簽存儲(chǔ)在data中的變量中
具體代碼如下:
<template>
<el-select v-model="selectedValue" placeholder="請(qǐng)選擇">
<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
</el-option>
</el-select>
<div>
<div>選擇的值:{{ selectedValue }}</div>
<div>對(duì)應(yīng)的標(biāo)簽:{{ selectedLabel }}</div>
</div>
</template>
<script>
export default {
data() {
return {
options: [
{ value: 'option1', label: '選項(xiàng)1' },
{ value: 'option2', label: '選項(xiàng)2' },
{ value: 'option3', label: '選項(xiàng)3' }
],
selectedValue: '',
selectedLabel: ''
};
},
watch: {
selectedValue(newVal) {
const option = this.options.find(item => item.value === newVal);
this.selectedLabel = option ? option.label : '';
}
}
};
</script>結(jié)果展示:

在template中,v-model指令綁定了selectedValue變量,表示選中的值。
同時(shí),給<el-option>添加了v-for循環(huán)生成所有的選項(xiàng)。
當(dāng)選中的值改變時(shí),使用watch監(jiān)聽selectedValue的變化,通過find方法從options中找到選中的值對(duì)應(yīng)的選項(xiàng),并將標(biāo)簽存儲(chǔ)在selectedLabel變量中。
最后,將selectedValue和selectedLabel顯示在頁面上。
2 @change事件獲取
2.1 只返回選擇的value
<template>
<div>
<el-select v-model="selectedValue" @change="getSelectValue">
<el-option
v-for="option in options"
:key="option.value"
:label="option.label"
:value="option.value"
>
</el-option>
</el-select>
</div>
</template>
<script>
export default {
data() {
return {
options: [
{ value: 'option1', label: '選項(xiàng)1' },
{ value: 'option2', label: '選項(xiàng)2' },
{ value: 'option3', label: '選項(xiàng)3' }
],
selectedValue: '',
};
},
methods: {
getSelectValue(data) {
console.log('value', data);
},
},
};
</script>結(jié)果展示:

2.2 返回選擇的value和label
下面是一個(gè)使用@change獲取element選擇框的值和名稱的Vue示例代碼:
<template>
<div>
<el-select v-model="selectedOption" @change="handleOptionChange">
<el-option
v-for="option in options"
:key="option.value"
:label="option.label"
:value="option.value"
>
</el-option>
</el-select>
<p>Selected Option: {{ selectedOption }}</p>
<p>Selected Option Label: {{ selectedOptionLabel }}</p>
</div>
</template>
<script>
export default {
data() {
return {
options: [
{ value: 'option1', label: '選項(xiàng)1' },
{ value: 'option2', label: '選項(xiàng)2' },
{ value: 'option3', label: '選項(xiàng)3' }
],
selectedOption: '',
selectedOptionLabel: '',
};
},
methods: {
handleOptionChange() {
this.selectedOptionLabel = this.options.find(
(option) => option.value === this.selectedOption
).label;
},
},
};
</script>結(jié)果展示:

在這個(gè)示例代碼中,我們首先定義了一個(gè)el-select元素,并使用v-model指令綁定了一個(gè)selectedOption變量,這個(gè)變量將用于存儲(chǔ)用戶選擇的選項(xiàng)的值。
接著,我們?cè)?code>el-select元素上添加了一個(gè)@change事件監(jiān)聽器,當(dāng)用戶在選擇框中選擇一個(gè)選項(xiàng)時(shí),該事件監(jiān)聽器會(huì)被觸發(fā)。
handleOptionChange方法是@change事件監(jiān)聽器的處理函數(shù),它通過使用find方法查找用戶選擇的選項(xiàng)的標(biāo)簽,并將其存儲(chǔ)在selectedOptionLabel變量中。
最后,我們?cè)谀0逯袑?code>selectedOption和selectedOptionLabel變量的值顯示出來。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- vue之elementUi的el-select同時(shí)獲取value和label的三種方式
- vue3?element?plus?table?selection展示數(shù)據(jù),默認(rèn)選中功能方式
- Vue?elementui如何實(shí)現(xiàn)表格selection的默認(rèn)勾選
- vue-treeselect(適配Vue3.2)及Element-plus的TreeSelect組件使用
- vue elementui select標(biāo)簽監(jiān)聽change事件失效問題
- vue2結(jié)合element-ui實(shí)現(xiàn)TreeSelect樹選擇功能
- Vue中使用 ElementUi 的 el-select 實(shí)現(xiàn)全選功能(思路詳解)
相關(guān)文章
vue中計(jì)算屬性computed和普通屬性method的區(qū)別小結(jié)
Vue.js中Computed和Methods是兩種常用的數(shù)據(jù)處理方式,本文主要介紹了vue中計(jì)算屬性computed和普通屬性method的區(qū)別小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
Vue項(xiàng)目實(shí)現(xiàn)換膚功能的一種方案分析
這篇文章主要介紹了Vue項(xiàng)目實(shí)現(xiàn)換膚功能的一種方案分析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
Vue移動(dòng)端下拉加載更多數(shù)據(jù)onload實(shí)現(xiàn)方法淺析
這篇文章主要介紹了Vue移動(dòng)端下拉加載更多數(shù)據(jù)onload實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
vue在自定義組件中使用v-model進(jìn)行數(shù)據(jù)綁定的方法
這篇文章主要介紹了vue在自定義組件中使用v-model進(jìn)行數(shù)據(jù)綁定的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03

