Vue+element Ui的el-select同时获取value和label的方法总结

1.通过ref的形式(推荐)

javascript 复制代码
<template>
  <div class="root">
    <el-select
      ref="optionRef"
      @change="handleChange"
      v-model="value"
      placeholder="请选择"
      style="width: 250px"
    >
      <el-option
        v-for="item in options"
        :key="item.id"
        :label="item.label"
        :value="item.value"
      >
      </el-option>
    </el-select>
    <el-button style="margin-left: 20px" @click="showoptions" type="primary" >查看</el-button >
  </div>
</template>
<script>
export default {
  data() {
    return {
      value: "",
      options: [
        { id: 0, label: "苹果", value: "apple" },
        { id: 1, label: "香蕉", value: "banana" },
        { id: 2, label: "橙子", value: "orange" },
      ],
    };
  },
  methods: {
    showoptions() {
      console.log(
        this.$refs.optionRef.selected.value,
        this.$refs.optionRef.selected.label
      );
    },
    handleChange(){
      // change 改变后获取值,要在nextTick中。
      this.$nextTick(function () {
        this.$refs.optionRef.selected.value,
        this.$refs.optionRef.selected.label
      });
    }
  },
};
</script>

2.通过字符串拼接的形式(推荐)

javascript 复制代码
<template>
  <div class="root">
    <el-select ref="optionRef" @change="handleChange" v-model="value" placeholder="请选择" style="width: 250px">
      <el-option v-for="item in options" :key="item.id" :label="item.label" :value="item.value">
      </el-option>
    </el-select>
    <el-button style="margin-left: 20px" @click="showoptions" type="primary">查看</el-button>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        value: "",
        options: [
          { id: 0, label: "苹果", value: "apple" },
          { id: 1, label: "香蕉", value: "banana" },
          { id: 2, label: "橙子", value: "orange" },
        ],
      };
    },
    methods: {
      showoptions() {
        console.log(this.value);
        console.log("value=====", this.value.split("+")[0]);
        console.log("label=====", this.value.split("+")[1]);
      },
      handleChange() {
        // change 改变后获取值,要在nextTick中。
        this.$nextTick(function () {
          console.log(this.value);
          console.log("value=====", this.value.split("+")[0]);
          console.log("label=====", this.value.split("+")[1]);
        });
      }
    },
  };
</script>

3.通过遍历的形式(不推荐)

javascript 复制代码
<template>
  <div class="root">
    <el-select ref="optionRef" @change="handleChange" v-model="value" placeholder="请选择" style="width: 250px">
      <el-option v-for="item in options" :key="item.id" :label="item.label" :value="item.value">
      </el-option>
    </el-select>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        value: "",
        options: [
          { id: 0, label: "苹果", value: "apple" },
          { id: 1, label: "香蕉", value: "banana" },
          { id: 2, label: "橙子", value: "orange" },
        ],
      };
    },
    methods: {
    
      handleChange(v) {
        // change 改变后获取值,要在nextTick中。
        this.$nextTick(function () {
          let obj = this.options.find(item=>item.value==v)
          console.log("value=====", obj.value);
          console.log("label=====", obj.label);
        });
      }
    },
  };
</script>
相关推荐
梵得儿SHI16 分钟前
Vue 高级特性:渲染函数与 JSX 精讲(h 函数语法、JSX 在 Vue 中的应用)
前端·javascript·vue.js·jsx·模板语法·渲染函数·底层视图生成机制
GGGG寄了18 分钟前
CSS——文字控制属性
前端·javascript·css·html
菜鸟茜23 分钟前
ES6核心知识解析01:什么是ES6以及为什么需要ES6
前端·javascript·es6
David凉宸25 分钟前
Vue 3 项目的性能优化策略:从原理到实践(页面展示)
javascript·vue.js·性能优化
摘星编程30 分钟前
在OpenHarmony上用React Native:ImageGIF动图播放
javascript·react native·react.js
摘星编程35 分钟前
React Native + OpenHarmony:Text文本高亮显示
javascript·react native·react.js
Betelgeuse7637 分钟前
【Flutter For OpenHarmony】 阶段复盘:从单页Demo到模块化App
flutter·ui·华为·交互·harmonyos
忧郁的Mr.Li1 小时前
设计模式--单例模式
javascript·单例模式·设计模式
摘星编程1 小时前
在OpenHarmony上用React Native:Text文本可点击链接
javascript·react native·react.js
Byron07071 小时前
基于 Vue 的微前端架构落地实战:从 0 到 1 搭建企业级多应用体系
前端·vue.js·架构