vue.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > vue.js > el-select获取下拉框选值

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

作者:Komorebi゛

在开发业务场景中我们通常遇到一些奇怪的需求,例如el-select业务场景需要同时获取我们选中的label跟 value,下面这篇文章主要给大家介绍了关于el-select如何获取下拉框选中label和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如何获取下拉框选中label和value值的文章就介绍到这了,更多相关el-select获取下拉框选值内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文