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 = ''
}
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Komorebi゛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值