elementUI select,option变化,如果option不存在上次的选项,自动清空上次的选择

select,option变化,如果option不存在上次的选项,自动清空上次的选择

javascript 复制代码
<script src="//unpkg.com/vue@2/dist/vue.js"></script>
<script src="//unpkg.com/element-ui@2.15.14/lib/index.js"></script>
<div id="app">
<template>
  <el-select v-model="value" placeholder="请选择">
    <el-option
      v-for="item in arr"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
  <el-button @click="fun">fun</el-button>
  <el-button @click="fun2">fun2</el-button>
  <el-button @click="fun3">fun3</el-button>
  <el-button @click="fun4">fun4</el-button>
</template>
</div>

var Main = {
    data() {
      return {
        arr:[],
        options: [{value: '黄金糕',label: '黄金糕'}, {
          value: '双皮奶',label: '双皮奶'}, {
          value: '蚵仔煎',label: '蚵仔煎'}],
        options2: [{value: '黄金糕2',label: '黄金糕2'}, {
          value: '双皮奶2',label: '双皮奶2'}, {
          value: '蚵仔煎2',label: '蚵仔煎2'} ,{
          value: '蚵仔煎',label: '蚵仔煎'}],
        value: ''//
      }
    },
    methods:{
      fun(){
        this.arr= this.options
      },
      fun2(){
        this.arr=this.options2
      },
      fun3(){ this.arr=[]},
      fun4(){ alert(this.value)}
    },
    watch:{
      arr(val){
        let data=val.find(i=>i.value==this.value)
        if(!data)this.value=''
      }
    }
  }
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
相关推荐
_阿南_7 小时前
项目中新增给AI制定的代码规范
前端·程序员
名字还没想好☜7 小时前
React 实现暗黑模式切换:localStorage 持久化、SSR 首屏闪烁与跟随系统主题
前端·javascript·react.js·ecmascript·react·next.js
console.log('npc')9 小时前
Git 冲突与 AI 协助指南
前端·人工智能·git·大模型
爱学堂IT分享10 小时前
Cesium可视化系统实战课程-Cesium教程学习
前端
糖墨夕10 小时前
理解大语言模型:Agent 的“大脑”
前端·agent
Rain的Java大神之路10 小时前
JavaWeb开发如何解决跨域问题
java·前端·后端·nginx·web安全·面试·运维开发
cpolar技术支持11 小时前
浏览器也能跑本地 AI:用 Transformers.js + WebGPU 做一个最小推理 Demo,cpolar 给同事远程体验
前端·ai·cpolar·webgpu·transformers.js
计算机毕设定制辅导-无忧学长11 小时前
《基于SpringBoot青年公寓出租管理系统的设计与实现》
java·vue.js·spring boot·青年公寓出租管理系统
计算机魔术师12 小时前
OpenAI 发布 GPT-6 Astra:多项基准刷新纪录, cybersecurity 能力达 Critical 阈值
前端
xy345312 小时前
Axure9.0中继器遮罩实现方法
前端·ui·html·axure·原型·产品设计