本文實例為大家分享了Vue實現省市區三級聯動的具體代碼,供大家參考,具體內容如下
效果圖:
代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
<!DOCTYPE html> < html > < head > < meta charset = "GBK" > < title ></ title > < style > </ style > </ head > < body > < div id = "app" > < select v-model = 'prov' @ change = "changeCity();changeCity1()" > < option v-for = "(item,i) in items" >{{item.name}}</ option > </ select > -- < select v-model = 'city' @ change = "changeCity1" > < option v-for = "(item,i) in cityArr" >{{item.name}}</ option > </ select > -- < select v-model = 'city1' > < option v-for = "(item,i) in cityArr1" >{{item.name}}</ option > </ select > < p >您選中的是:{{prov}}-{{city}}-{{city1}}</ p > </ div > </ body > < script src = "vue.js" ></ script > < script > var id=1; new Vue({ el:'#app', data:{ prov:'北京', city:'', city1:'', items:[ {name:'北京', sub:[ {name:'北京市',sub:[{name:'北京市11'},{name:'北京市12'},{name:'北京市13'}]}, {name:'北京市2',sub:[{name:'北京市21'},{name:'北京市22'},{name:'北京市23'}]}, ] }, {name:'江西', sub:[ {name:'南昌市',sub:[{name:'高新區'},{name:'西湖區'},{name:'瑤湖區'}]}, {name:'贛州市',sub:[{name:'贛州市1'},{name:'贛州市2'},{name:'贛州市3'}]}, {name:'撫州市',sub:[{name:'撫州市1'},{name:'撫州市2'},{name:'撫州市3'}]} ] } ], cityArr:[], cityArr1:[] }, mounted:function(){//執行默認選擇 this.changeCity(); this.changeCity1(); }, methods:{ changeCity:function(){//省切換 var me=this; var item ; me.items.forEach(function(ele){ if(ele.name===me.prov){//判斷與prov是否相等,相等的表示被切換選中的省份 item = ele; } }) if(item){ this.cityArr=item.sub;//將選中的item的sub設置給cityArr 用于顯示市 this.city=item.sub[0].name;//默認選擇第一個市 this.cityArr1=[]; this.city1=''; } }, changeCity1:function(){//市切換 var me=this; var item ; me.cityArr.forEach(function(ele){ if(ele.name===me.city){//判斷與city是否相等,相等的表示被切換選中的市 item = ele; } }) if(item){ this.cityArr1=item.sub;//將選中的item的sub設置給cityArr1 用于顯示區 this.city1=item.sub[0].name;//默認選擇第一個區 } } } }) </ script > </ html > |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/dkm123456/article/details/111618000