VUE+elementUI前端实现 三级省市区联动select

HTML代码
<b-col md="6">
	<label >省</label>
	<el-select v-model="provinceValue" placeholder="请选择省" @change="chooseProvince">
		<el-option	
    		v-for="item in provinceData"
    		:key="item.code"
    		:label="item.name"
    		:value="item.name">
		</el-option>
	</el-select>
</b-col>
<b-col md="6">
	<label >市</label>
	<el-select v-model="cityValue" placeholder="请选择市" @change="chooseCity">
		<el-option	
    		v-for="item in cityData"
    		:key="item.code"
    		:label="item.name"
    		:value="item.name">
		</el-option>
	</el-select>
</b-col>
<b-col md="6">
	<label >区、县</label>
	<el-select v-model="areaValue" @change="chooseArea" placeholder="请选择区、县">
    	<el-option
	        v-for="item in areaData"
	        :key="item.code"
	        :label="item.name"
	        :value="item.name">
	    </el-option>
    </el-select>
</b-col>
JS代码
	import axios from 'axios';
	export default{
		data(){
			return {
				provinceValue:'',
				cityValue:'',
				areaValue:'',
				provinceData:[],
				cityData:[],
				areaData:[],
				
			}
		},
		created(){
			axios.get('http://47.99.104.19:8001/ChinaCity.json').then(res=>{
				this.provinceData = res.data;
			}).catch(e => {
				this.$message.error("网络连接超时");
		    })
		},
		methods:{
			chooseProvince(value){
				this.cityValue = '';
				this.areaValue = '';
				this.cityData = [];
				this.areaData = [];
				this.provinceData.map(e=>{//遍历数据
					if( value == e.name){
						this.cityData = e.cityList;
						return;
					}
				})
			},
			chooseCity(value){
				this.areaValue = '';
				this.cityData.map(e=>{//遍历数据
					if( value == e.name){
						this.areaData = e.areaList;
						return;
					}
				})
			},
			chooseArea(){
				
			}
		}
	}
	//JSON数据需要的请自行下载 谢谢
Logo

前往低代码交流专区

更多推荐