首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

JS 城市选择实现——按级选中省市县/区

  • 23-11-14 10:01
  • 4565
  • 8293
blog.csdn.net

来自网络:

https://www.sunzhongwei.com/js-provinces-three-level-linkage-selection-component?from=sidebar_new

效果图

实现

项目结构

city.js

http://iyenn.com/rec/1648190.html

index.html

  1. html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>地址选择title>
  6. <link rel="stylesheet" href="style.css">
  7. head>
  8. <body>
  9. <div>
  10. <fieldset>
  11. <form action="#">
  12. <label for="addr-show">您选择的是:
  13. <input type="text" value="" id="addr-show">
  14. label>
  15. <br/>
  16. <select id="prov" onchange="showCity(this)">
  17. <option>=请选择省份=option>
  18. select>
  19. <select id="city" onchange="showCountry(this)">
  20. <option>=请选择城市=option>
  21. select>
  22. <select id="country" onchange="selecCountry(this)">
  23. <option>=请选择县区=option>
  24. select>
  25. <button type="button" class="btn met1" onClick="showAddr()" id="button-show" >确定button>
  26. form>
  27. fieldset>
  28. div>
  29. <script src="city.js">script>
  30. <script src="method.js">script>
  31. body>
  32. html>

method.js 

  1. //****************针对第二种方式的具体js实现部分******************//
  2. //****************所使用的数据是city.js******************//
  3. /*根据id获取对象*/
  4. function $(str) {
  5. return document.getElementById(str);
  6. }
  7. var addrShow02 = $('addr-show02'); //最终地址显示框
  8. var titleWrap = $('title-wrap').getElementsByTagName('LI');
  9. var addrWrap = $('addr-wrap'); //省市区显示模块
  10. var btn2 = document.getElementsByClassName('met2')[0]; //确定按钮
  11. var current2 = {
  12. prov: '',
  13. city: '',
  14. country: '',
  15. provVal: '',
  16. cityVal: '',
  17. countryVal: ''
  18. };
  19. /*自动加载省份列表*/
  20. window.onload = showProv2();
  21. function showProv2() {
  22. addrWrap.innerHTML = '';
  23. /*addrShow02.value = '';*/
  24. btn2.disabled = true;
  25. titleWrap[0].className = 'titleSel';
  26. var len = provice.length;
  27. for (var i = 0; i < len; i++) {
  28. var provLi = document.createElement('li');
  29. provLi.innerText = provice[i]['name'];
  30. provLi.index = i;
  31. addrWrap.appendChild(provLi);
  32. }
  33. }
  34. /*************************需要给动态生成的li绑定点击事件********************** */
  35. addrWrap.onclick = function (e) {
  36. var n;
  37. var e = e || window.event;
  38. var target = e.target || e.srcElement;
  39. if (target && target.nodeName == 'LI') {
  40. /*先判断当前显示区域显示的是省市区的那部分*/
  41. for (var z = 0; z < 3; z++) {
  42. if (titleWrap[z].className == 'titleSel')
  43. n = z;
  44. }
  45. /*显示的处理函数*/
  46. switch (n) {
  47. case 0:
  48. showCity2(target.index);
  49. break;
  50. case 1:
  51. showCountry2(target.index);
  52. break;
  53. case 2:
  54. selectCountry(target.index);
  55. break;
  56. default:
  57. showProv2();
  58. }
  59. }
  60. };
  61. /*选择省份之后显示该省下所有城市*/
  62. function showCity2(index) {
  63. addrWrap.innerHTML = '';
  64. current2.prov = index;
  65. current2.provVal = provice[index].name;
  66. titleWrap[0].className = '';
  67. titleWrap[1].className = 'titleSel';
  68. var cityLen = provice[index].city.length;
  69. for (var j = 0; j < cityLen; j++) {
  70. var cityLi = document.createElement('li');
  71. cityLi.innerText = provice[index].city[j].name;
  72. cityLi.index = j;
  73. addrWrap.appendChild(cityLi);
  74. }
  75. }
  76. /*选择城市之后显示该城市下所有县区*/
  77. function showCountry2(index) {
  78. addrWrap.innerHTML = '';
  79. current2.city = index;
  80. current2.cityVal = provice[current2.prov].city[index].name;
  81. titleWrap[1].className = '';
  82. titleWrap[2].className = 'titleSel';
  83. var countryLen = provice[current2.prov].city[index].districtAndCounty.length;
  84. if (countryLen == 0) {
  85. addrShow02.value = current2.provVal + '-' + current2.cityVal;
  86. }
  87. for (var k = 0; k < countryLen; k++) {
  88. var cityLi = document.createElement('li');
  89. cityLi.innerText = provice[current2.prov].city[index].districtAndCounty[k];
  90. cityLi.index = k;
  91. addrWrap.appendChild(cityLi);
  92. }
  93. }
  94. /*选中具体的县区*/
  95. function selectCountry(index) {
  96. btn2.disabled = false;
  97. current2.country = index;
  98. addrWrap.getElementsByTagName('li')[index].style.backgroundColor = '#23B7E5';
  99. current2.countryVal = provice[current2.prov].city[current2.city].districtAndCounty[index];
  100. }
  101. /*点击确定后恢复成初始状态,且将所选地点显示在输入框中*/
  102. btn2.onclick = function () {
  103. addrShow02.value = current2.provVal + ' ' + current2.cityVal + ' ' + current2.countryVal;
  104. addrWrap.getElementsByTagName('li')[current2.country].style.backgroundColor = '';
  105. };
  106. /*分别点击省市区标题的处理函数*/
  107. document.getElementById('title-wrap').onclick = function (e) {
  108. var e = e || window.event;
  109. var target = e.target || e.srcElement;
  110. if (target && target.nodeName == 'LI') {
  111. for (var z = 0; z < 3; z++) {
  112. titleWrap[z].className = '';
  113. }
  114. target.className = 'titleSel';
  115. if (target.value == '0') {
  116. showProv2();
  117. } else if (target.value == '1') {
  118. showCity2(current2.prov);
  119. } else {
  120. showCountry2(current2.city);
  121. }
  122. }
  123. };

style.css

  1. /*全局样式*/
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. }
  6. fieldset {
  7. width: 500px;
  8. padding: 20px;
  9. margin: 30px;
  10. border: 1px solid #ccc;
  11. }
  12. legend{
  13. font-size: 18px;
  14. font-weight: bold;
  15. }
  16. #addr-show, #addr-show02,#addr-show03{
  17. width: 200px;
  18. height: 25px;
  19. margin-bottom: 10px;
  20. }
  21. .btn {
  22. width: 80px;
  23. height: 30px;
  24. border-radius: 4px;
  25. border: 1px solid #ccc;
  26. outline: none;
  27. background-color: #aaa;
  28. margin: 0 20px;
  29. }
  30. .btn:disabled{
  31. background-color:#ccc;
  32. }
  33. #addr-choice{
  34. border-top: 1px solid #ccc;
  35. border-bottom: 1px solid #ccc;
  36. padding: 10px 0;
  37. margin-bottom: 20px;
  38. }
  39. #title-wrap li,#addr-wrap li{
  40. list-style: none;
  41. display: inline-block;
  42. text-align: center;
  43. cursor: pointer;
  44. }
  45. #title-wrap li{
  46. width:163px;
  47. height: 45px;
  48. line-height: 45px;
  49. margin-bottom: 10px;
  50. }
  51. .titleSel{
  52. border-bottom: 2px solid #23B7E5;
  53. }
  54. #addr-wrap li{
  55. width: 83px;
  56. height: 30px;
  57. border-radius: 4px;
  58. line-height: 30px;
  59. font-size: 14px;
  60. overflow:hidden;
  61. white-space:nowrap;
  62. text-overflow:ellipsis;
  63. }
  64. #addr-wrap li:hover{
  65. background-color: #23B7E5;
  66. }

 

注:本文转载自blog.csdn.net的Ms_WangH的文章"https://blog.csdn.net/qq_26416195/article/details/81161741"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top