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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="./js/vue.3.3.4.js"></script> <style> select, option { width: 120px; } </style> </head> <body> <div id="box"> 省份:<select v-model="province"> <option :value="province.name" v-for="province in province_list">{{province.name}}</option> </select> 城市:<select v-model="city"> <option :value="city.name" v-for="city in citys">{{city.name}}</option> </select> 地区:<select v-model="dist"> <option :value="dist.name" v-for="dist in dists">{{dist.name}}</option> </select> </div> <script> vm = Vue.createApp({ data() { return { dists: [], provinces: [], citys: [], dist: '', province: '', city: '', province_list: [ {'id': 1, 'name': '江西省'}, {'id': 2, 'name': '浙江省'}, {'id': 3, 'name': '湖南省'}, {'id': 4, 'name': '广东省'}, ], city_list: [ {'id': 5, 'name': '抚州市', "pid": 1}, {'id': 6, 'name': '九江市', "pid": 1}, {'id': 7, 'name': '鹰潭市', "pid": 1}, {'id': 8, 'name': '上饶市', "pid": 1}, {'id': 9, 'name': '南昌市', "pid": 1}, {'id': 10, 'name': '杭州市', "pid": 2}, {'id': 11, 'name': '温州市', "pid": 2}, {'id': 12, 'name': '宁波市', "pid": 2}, {'id': 13, 'name': '金华市', "pid": 2}, {'id': 14, 'name': '台州市', "pid": 2}, {'id': 15, 'name': '长沙市', "pid": 3}, {'id': 16, 'name': '湘潭市', "pid": 3}, {'id': 17, 'name': '广州市', "pid": 3}, {'id': 18, 'name': '深圳市', "pid": 4}, {'id': 19, 'name': '汕头市', "pid": 4}, ], dist_list: [ {"id": 20, "name": '芙蓉区', "pid": 15}, {"id": 21, "name": '天心区', "pid": 15}, {"id": 22, "name": '临川区', "pid": 5}, {"id": 23, "name": '金溪县', "pid": 5}, {"id": 24, "name": '龙湾区', "pid": 11}, {"id": 25, "name": '瓯海区', "pid": 11}, {"id": 25, "name": '越秀区', "pid": 17}, ] } }, watch: { province() { this.city='' let province_id = -1; for (let provice of this.province_list) { if (provice.name === this.province) { province_id = provice.id; break; } } this.citys = [] for (let key of this.city_list) { if (key.pid === parseInt(province_id)) { this.citys.push(key); } } }, city() { let city_id = -1; for (let city of this.city_list) { if (city.name === this.city) { city_id = city.id; break; } } this.dists = [] let count = false for (let key of this.dist_list) { if (key.pid === parseInt(city_id)) { this.dists.push(key); count = true } }
} }, }).mount("#box") </script> </body> </html>
|