task-comp1.html 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>城市选择</title>
  5. <style>
  6. .select-container {
  7. display: inline-block;
  8. margin-right: 10px;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <div id="app">
  14. <div class="select-container">
  15. <select v-model="selectedProvince" @change="onProvinceChange">
  16. <option value="">请选择省份</option>
  17. <option v-for="province in provinces" :value="province">{{ province }}</option>
  18. </select>
  19. </div>
  20. <div class="select-container">
  21. <select v-model="selectedCity" @change="onCityChange">
  22. <option value="">请选择城市</option>
  23. <option v-for="city in cities" :value="city">{{ city }}</option>
  24. </select>
  25. </div>
  26. <div class="select-container">
  27. <select v-model="selectedDistrict">
  28. <option value="">请选择区域</option>
  29. <option v-for="district in districts" :value="district">{{ district }}</option>
  30. </select>
  31. </div>
  32. </div>
  33. <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script>
  34. <script>
  35. new Vue({
  36. el: '#app',
  37. data: {
  38. provinces: ['江西省', '浙江省', '江苏省'],
  39. cities: {
  40. '江西省': '南昌市',
  41. '浙江省': '杭州市',
  42. '江苏市': '南京市'
  43. },
  44. districts: {
  45. '南昌市': ['东湖区', '西湖区', '青山湖区'],
  46. '上海市': ['黄浦区', '徐汇区', '静安区'],
  47. '北京市': ['东城区', '西城区', '朝阳区']
  48. },
  49. selectedProvince: '',
  50. selectedCity: '',
  51. selectedDistrict: ''
  52. },
  53. methods: {
  54. onProvinceChange() {
  55. this.selectedCity = '';
  56. this.selectedDistrict = '';
  57. },
  58. onCityChange() {
  59. this.selectedDistrict = '';
  60. }
  61. }
  62. });
  63. </script>
  64. </body>
  65. </html>