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參數(shù)傳遞過去;
如果想要傳遞多個值,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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
el-table表頭使用el-dropdown出現(xiàn)兩個下拉框的問題及解決方法
本文給大家分享el-table在固定右邊列時,表頭使用el-dropdown會出現(xiàn)兩個下拉框的解決方法,感興趣的朋友跟隨小編一起看看吧2024-07-07
vue+vant使用圖片預覽功能ImagePreview的問題解決
這篇文章主要介紹了vue+vant使用圖片預覽功能ImagePreview的問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04
vue-loader和webpack項目配置及npm錯誤問題的解決
這篇文章主要介紹了vue-loader和webpack項目配置及npm錯誤問題的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
Vue使用Axios庫請求數(shù)據時跨域問題的解決方法詳解
在 VUE 項目開發(fā)時,遇到個問題,正常設置使用 Axios 庫請求數(shù)據時,報錯提示跨域問題,那在生產壞境下,該去怎么解決呢?下面小編就來和大家詳細講講2024-01-01
vue+element多選級聯(lián)選擇器自定義props使用詳解
這篇文章主要給大家介紹了關于vue+element多選級聯(lián)選擇器自定義props使用的相關資料,級聯(lián)選擇器展示的結果都是以數(shù)組的形式展示,也就是v-model綁定的結果,需要的朋友可以參考下2023-07-07

