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>
相关推荐
海兰2 小时前
【应用】基于 Next.js 16 + Python mplfinance的金融K线图与技术指标可视化平台(三)
javascript·python·金融
中国软件测试质量协会3 小时前
奇林测试平台(kylinTOP)UI自动化测试能力深度解析
ui
小磊哥er3 小时前
深入解构Claude Code - 第 7 篇 · 连接外面的世界
javascript·ai编程
小磊哥er4 小时前
深入解构Claude Code - 第 6 篇 · 终端里的图形界面
javascript·ai编程
海兰4 小时前
【应用】基于 Next.js 16 + Python mplfinance的金融K线图与技术指标可视化平台(一)
javascript·python·金融
可乐鸡翅yeah_5 小时前
hls.js 内存泄漏实战排查,直播 M3U8 长时间播放异常定位方案
开发语言·javascript·python·django·ecmascript·m3u8·m3u8在线
可乐鸡翅yeah_5 小时前
第三方接口对接 M3U8 流媒体排坑实战,解决外部服务商流兼容难题
前端·javascript·python·django·html·m3u8·m3u8在线
默_笙6 小时前
🏠 「LLM Notes」:我用 Next.js + Redis 给自己造了个笔记博客
前端·javascript
ruanCat7 小时前
Prettier 明明执行成功,Markdown 为什么还是没变?一次插件误诊复盘
前端·javascript·node.js
软件开发技术深度爱好者8 小时前
在线学习实践 TypeScript 的官方Playground(演练场)使用介绍
javascript·学习·typescript