效果
代碼實現
<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>