el-select如何获取下拉框选中label和value的值

Iria ·
更新时间:2024-09-20
· 374 次阅读

目录

【示例1】

【示例2】

【示例3】

总结

【示例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如何获取下拉框选中label和value值的文章就介绍到这了,更多相关el-select获取下拉框选值内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



value label 下拉框 select

需要 登录 后方可回复, 如果你还没有账号请 注册新账号