el-select如何獲取下拉框選中l(wèi)abel和value的值
更新時間:2022年10月20日 09:05:55 作者:Komorebi゛
在開發(fā)業(yè)務場景中我們通常遇到一些奇怪的需求,例如el-select業(yè)務場景需要同時獲取我們選中的label跟 value,下面這篇文章主要給大家介紹了關于el-select如何獲取下拉框選中l(wèi)abel和value的值,需要的朋友可以參考下
【示例1】
<templete slot-scope="scope">
<el-form-item :prop="'list'. + scope.$index + '.goodModularId'">
<!-- change事件中,會獲取到當前選中的值(因為默認會將event參數傳遞過去;
如果想要傳遞多個值,change($event, "傳遞的其他值"),將“選中的當前元素” 和 “傳遞的其他值” 一起傳遞過去 -->
<el-select v-model="ruleForm.goodModularId" @change="getModularValue($event, scope.$index)" @clear="delModularValue(scope.$index)">
<el-option v-for="(item,index) in modularData" :key="index" :value="item.id" :label="item.name"></el-option>
</el-select>
</el-form-item>
</templete>
<script>
data() {
return {
ruleForm: {
list: [{
goodModularId: '',
goodModular: ''
}]
}
}
}
methods: {
// 獲取value值給goodModular
getModularValue(val,index) {
let obj = this.modularData.find(item => item.id === val)
// 判斷的時候可以直接寫obj而不需要以判斷對象是否為空的方式是因為:如果找不到,find方法返回的是undefined而不是空對象
if(obj) {
this.ruleForm.list[index].goodModular = obj.name
} else {
this.ruleForm.list[index].goodModular = ''
}
}
// 清空選項事件
delModularValue(index) {
this.ruleForm.list[index].goodModular = ''
}
}
</script>
【示例2】
<templete slot-scope="scope">
<el-form-item :prop="'list'. + scope.$index + '.goodModularId'">
<el-select v-model="ruleForm.goodModularId" @clear="delModularValue(scope.$index)">
<el-option v-for="(item,index) in modularData" :key="index" :value="item.id" :label="item.name" @click.native="getModularValue(item.id, scope.$index)"></el-option>
</el-select>
</el-form-item>
</templete>
<script>
data() {
return {
ruleForm: {
list: [{
goodModularId: '',
goodModular: ''
}]
}
}
}
methods: {
getModularValue(val,index) {
let obj = this.modularData.find(item => item.id === val)
if(obj) {
this.ruleForm.list[index].goodModular = obj.name
} else {
this.ruleForm.list[index].goodModular = ''
}
},
delModularValue(index) {
this.ruleForm.list[index].goodModular = ''
}
}
</script>
【示例3】
<el-form-item label="類別" prop="categoryId"> <el-select v-model="ruleForm.categoryId" @clear="clearCategory"> <el-option v-for="(item,index) in categoryOptions" :key="item.id" :value="item.id" :label="item.name" @click.native="getValue(item.name, categoryName)"></el-option> </el-select> </el-form-item>
getValue(val, type) {
this.ruleForm[type] = val
}
clearCategory() {
this.ruleForm.categoryName = ''
}
總結
到此這篇關于el-select如何獲取下拉框選中l(wèi)abel和value值的文章就介紹到這了,更多相關el-select獲取下拉框選值內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于vue監(jiān)聽滾動事件實現(xiàn)錨點鏈接平滑滾動的方法
本篇文章主要介紹了基于vue監(jiān)聽滾動事件實現(xiàn)錨點鏈接平滑滾動的方法,非常具有實用價值,有興趣的可以了解一下2018-01-01
Vue3中reactive變量重新賦值無法響應的3種處理方法
這篇文章主要給大家介紹了關于Vue3中reactive變量重新賦值無法響應的3種處理方法,在Vue3中可以使用reactive函數將一個普通對象轉換為響應式對象,需要的朋友可以參考下2023-08-08
vue開發(fā)中如何在js文件里使用pinia和組件同步
在JavaScript文件中封裝涉及到使用Pinia的方法時,發(fā)現(xiàn)Pinia和組件內容并不同步,二者的狀態(tài)是獨立的,解決辦法是,在新建對象的時候,將Pinia作為參數傳入,本文給大家介紹vue開發(fā)中如何在js文件里使用pinia和組件同步,感興趣的朋友一起看看吧2024-10-10

