vue + elementUI 實現下拉框二級聯動


效果

代碼實現

<template>
  <div class="app">
    <el-form ref="form" :model="form">
      <el-form-item label="手機品牌">
        <el-select v-model="form.phoneBrand" placeholder="請選擇" @change="changeSelect">
          <el-option
            v-for="(item,index) in brandOptions"
            :key="index"
            :label="item"
            :value="item"
          />
        </el-select>

      </el-form-item>
      <el-form-item label="手機型號">
        <el-select v-model="form.phoneType" placeholder="請選擇">
          <el-option
            v-for="(item,index) in typeOptions"
            :key="index"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      brandOptions: ['OPPO', '華為', 'VIVO'],
      brandObj: { 'OPPO': ['OPPO1', 'OPPO2', 'OPPO3'], '華為': ['華為1', '華為2', '華為3'], 'VIVO': ['VIVO1', 'VIVO2', 'VIVO3'] },
      typeOptions: [],
      form: {
        phoneBrand: '',
        phoneType: ''
      }
    }
  },
  methods: {
    changeSelect() {
      // 清空手機型號內容
      this.form.phoneType = ''

      // 遍歷手機品牌的下拉選項數組
      for (const k in this.brandOptions) {
        // 手機品牌內容 是否等於 手機品牌的下拉選擇數組中的某一項
        if (this.form.phoneBrand === this.brandOptions[k]) {
          this.typeOptions = this.brandObj[this.form.phoneBrand]
        }
      }
    }
  }
}
</script>
<style>
.app{
  margin:20px;
  width: 100%;
}
</style>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM