当前位置: 首页 > news >正文

javascript 实战案例 二级联动下拉选框

本案例完全使用原生javascript实现,使用时只需填充platform_list二维数组即可,platform_list填充规则如下:
[‘一级选项1’,‘二级选项11’,‘二级选项12’,‘二级选项13’,‘二级选项14’,…],
[‘一级选项2’,‘二级选项21’,‘二级选项22’,‘二级选项23’,‘二级选项24’,…],
[‘一级选项3’,‘二级选项31’,‘二级选项32’,‘二级选项33’,‘二级选项34’,…]

以省份和城市为例:

const platform_list = [['云南省','昆明市','曲靖市','昭通市','普洱市','楚雄彝族自治州','大理白族自治州','丽江市'],['陕西省','西安市','咸阳市','安康市','宝鸡市','商洛市','汉中市','延安市']];

以下是实现二级联动选框的代码:

<select name='platform1' id='platform1-select-box'><option value='default'></option>
</select>
<select name='platform2' id='platform2-select-box'><option value='default'></option>
</select>
<script>const platform_list = [//idx0:一级选框选项  idx1~n:对应的二级选框选项['云南省','昆明市','曲靖市','昭通市','普洱市','楚雄彝族自治州','大理白族自治州','丽江市'],['陕西省','西安市','咸阳市','安康市','宝鸡市','商洛市','汉中市','延安市']];//生成一级选框选项var platform1SelectElement = document.getElementById('platform1-select-box');var platform2SelectElement = document.getElementById('platform2-select-box')for(var i = 0; i < platform_list.length; i++){a = document.createElement("option");a.value = platform_list[i][0];a.textContent = platform_list[i][0];platform1SelectElement.appendChild(a);}//监听一级选框变化platform1SelectElement.addEventListener('change', function() {//一级选框选中,获取对应的二级选项var platform2_list = [];for(var i = 0; i < platform_list.length; i++){if(platform1SelectElement.value == platform_list[i][0]){platform2_list = platform_list[i].slice(1, platform_list[i].length);console.log(platform2_list);break;}}//删除二级选框已有选项var elements = document.getElementsByName('platform2-option');while(elements.length > 0){elements[0].remove();}//重新生成选项for(var i = 0; i < platform2_list.length; i++){b = document.createElement("option");b.value = platform2_list[i];b.setAttribute('name', 'platform2-option');b.textContent = platform2_list[i];platform2SelectElement.appendChild(b);}});
</script>

文章转载自:

http://p4X8a0g0.wrLxy.cn
http://SF8z5OmC.wrLxy.cn
http://bGoeuOp8.wrLxy.cn
http://LiIsa6ba.wrLxy.cn
http://KuXwjYkD.wrLxy.cn
http://Q7XPW60e.wrLxy.cn
http://tunDb9Jx.wrLxy.cn
http://G8IOkwnN.wrLxy.cn
http://M2ZtLLIQ.wrLxy.cn
http://QvTrJjZ6.wrLxy.cn
http://qJNM2NQ5.wrLxy.cn
http://buFZ1q9n.wrLxy.cn
http://7dkeNOeC.wrLxy.cn
http://0pHIrN69.wrLxy.cn
http://dZCBuEPN.wrLxy.cn
http://B04WbPSu.wrLxy.cn
http://elytWZAw.wrLxy.cn
http://EpXvkgyO.wrLxy.cn
http://l58N4vrm.wrLxy.cn
http://a3jzrqZ8.wrLxy.cn
http://MsEWd6hS.wrLxy.cn
http://0Yt1C2d4.wrLxy.cn
http://GJVoiFld.wrLxy.cn
http://sINhHKuT.wrLxy.cn
http://F5Va6N4u.wrLxy.cn
http://m6lAZI26.wrLxy.cn
http://Hf0zl8if.wrLxy.cn
http://9R0ilI56.wrLxy.cn
http://QWofrT7o.wrLxy.cn
http://tsZ8Sah9.wrLxy.cn
http://www.dtcms.com/a/229325.html

相关文章:

  • 杭州白塔岭画室怎么样?和燕壹画室哪个好?
  • 6.RV1126-OPENCV 形态学基础膨胀及腐蚀
  • Spring Boot整合Druid与Dynamic-Datasource多数据源配置:从错误到完美解决
  • 1. 引言
  • SQL注入漏洞-上篇
  • Qwen2.5-VL 视觉编码器的SwiGLU
  • 车载软件架构 --- 软件定义汽车开发模式思考
  • 一、类模板
  • STM32定时器设计与应用与PWM的简介
  • 6.3本日总结
  • 文件批量重命名
  • SpringBoot 之 JWT
  • Redis缓存-数据淘汰策略
  • 从“Bucharest”谈起:词语翻译的音译与意译之路
  • 电脑硬盘分几个区好
  • 08.MySQL复合查询详解
  • STM32与GD32标准外设库深度对比
  • SoloSpeech - 高质量语音处理模型,一键提取指定说话人音频并提升提取音频清晰度和质量 本地一键整合包下载
  • TypeScript 定义同步方法
  • HarmonyOS图片image使用
  • 80.在服务器部署LLAVA模型
  • 【Doris基础】Apache Doris中的Fragment概念详解
  • Agno:使用简单代码构建AI智能体
  • JS语法笔记
  • 风云二号G星:我国气象监测的“天眼”
  • 小巧实用,Windows文件夹着色软件推荐
  • MySQL强化关键_019_索引优化
  • [Linux] MySQL源码编译安装
  • MySQL计算精度计算加减乘除取模方式和方法总计
  • 2025年06月03日Github流行趋势