当前位置:网站首页>js原生代码实现三级联动效果
js原生代码实现三级联动效果
2022-04-22 12:25:00 【云墨-款哥的博客】
js原生代码实现三级联动效果
在很多网站中我们都会遇到一个模块,比如当我们注册信息填家庭地址的时候,或者我们进入一个商城的本地模式的时候,还有我们填写快递地址的时候,当我们选择了国家,对应的省份就会在下一个框中出现,我们选择了省份,城市就会在下一个框中出现,以此类推
有关三级联动的问题,我还用vue的computed属性写了一个vue版本,大家可以看一下这篇博客Vue版三级联动
这是我用到的数据,具体的数据文件我已经上传至我的资源库city_code.js
<script src="./city_code.js"></script>
下面是唯品会上的一个例子截图,我们选择了河南省之后,下一栏城市中就会出现河南的地市,并且默认选中郑州


下面是简单的HTML结构,在这里我使用了三个select下拉框来表示对应的省、市、区
并给他们添加对应的id
<body>
<select name="" id="province"></select>
<select name="" id="city"></select>
<select name="" id="area"></select>
</body>
**第一开始我们先给三个下拉框赋初识值,在初始状态下,我们应当显示的是北京市,市辖区,东城区,即每一个数据数组中的对应的第一个这里要着重提示一下:
- selectedIndex这个属性表示的是选中的option的索引,默认选中的索引都为零**
// 获取元素
var province = document.getElementById('province');
var city = document.getElementById('city');
var area = document.getElementById('area');
// 初始状态
// 插入省 遍历data数组
for (let i = 0; i < data.length; i++) {
var option = document.createElement('option');
option.innerHTML = data[i].name;
province.appendChild(option);
}
// 插入市 遍历city数组
var provinceId = province.selectedIndex;
for (let i = 0; i < data[provinceId].city.length; i++) {
var option = document.createElement('option');
option.innerHTML = data[provinceId].city[i].name;
city.appendChild(option);
}
// 插入区 遍历area数组
var cityId = city.selectedIndex;
for (let i = 0; i < data[provinceId].city[cityId].area.length; i++) {
var option = document.createElement('option');
option.innerHTML = data[provinceId].city[cityId].area[i].name;
area.appendChild(option);
}
**当我们点击选择对应的省份时,我们的第二个select下的option应该为我们选中的省份下的对应城市,区域应该变成该省份的第一个城市下的所有区(默认),因此,我们给省对应的select绑定onchange事件,这里要着重提示一下:
- selectedIndex这个属性表示的是选中的option的索引
2.每次更新之前要清空原有的数据 **
// 给表示省份的select绑定onchange事件
province.onchange = function(){
// 清空表示城市的select中原有的内容
city.innerHTML = '';
// 清空表示区的select中原有的内容
area.innerHTML = '';
// province.selectedIndex 当前选中的option的索引
provinceId = province.selectedIndex;
// 更新市
if (data[provinceId].city) {
for (let i = 0; i < data[provinceId].city.length; i++) {
var option = document.createElement('option');
option.innerHTML = data[provinceId].city[i].name;
city.appendChild(option);
}
// 更新区
for (let i = 0; i < data[provinceId].city[cityId].area.length; i++) {
var option = document.createElement('option');
option.innerHTML = data[provinceId].city[cityId].area[i].name;
area.appendChild(option);
}
}
}
当我们点击选择对应的城市时,我们的第三个select下的option应该为我们选中的城市下的对应省份
// 给表示城市的select绑定onchange事件
city.onchange = function(){
// 清空表示区的select中原有的内容
area.innerHTML = '';
// 更新区
for (let i = 0; i < data[provinceId].city[city.selectedIndex].area.length; i++) {
var option = document.createElement('option');
option.innerHTML = data[provinceId].city[city.selectedIndex].area[i].name;
area.appendChild(option);
}
}
版权声明
本文为[云墨-款哥的博客]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_56026872/article/details/116058188
边栏推荐
- 电工第二讲
- [concurrent programming 050] type and description of memory barrier?
- 购物表格制作
- 【并发编程053】双重检查锁的变量为什么使用volatile变量
- Machine learning training template, summarizing multiple classifiers
- ROS2学习笔记(五)从turtlesim学习ROS2参数
- LeetCode 118、杨辉三角
- [in depth understanding of tcallusdb technology] example code - asynchronous call interface
- Electrician Lecture 1
- Esp32-cam usage history
猜你喜欢
![[in depth understanding of tcallusdb technology] data interface description for reading the specified location in the list - [list table]](/img/ed/cccd5dee09d2f0a3e6c788bd265b36.png)
[in depth understanding of tcallusdb technology] data interface description for reading the specified location in the list - [list table]
![[in depth understanding of tcallusdb technology] example code - asynchronous call interface](/img/ed/cccd5dee09d2f0a3e6c788bd265b36.png)
[in depth understanding of tcallusdb technology] example code - asynchronous call interface

Read attention concatenation volume for accurate and efficient stereo matching

ROS2学习笔记(五)从turtlesim学习ROS2参数

LeetCode 617、合并二叉树
![[concurrent programming 055] will the following daemon thread execute the code in the finally module?](/img/e8/92d33e701d3b1691e30885d4111315.png)
[concurrent programming 055] will the following daemon thread execute the code in the finally module?

. net treasure API: outputformatter, format output object

Design and implementation of house leasing system based on J2EE Rar (paper + project source code + database file)

可以测量50A以上电流的隔离集成式电流传感器CH704应用案例分享
![[in depth understanding of tcallusdb technology] scan data interface description - [list table]](/img/ed/cccd5dee09d2f0a3e6c788bd265b36.png)
[in depth understanding of tcallusdb technology] scan data interface description - [list table]
随机推荐
一种自动切换过流保护模块的热泵装置保护电路介绍(ACS758/CH704应用案例)
【深入理解TcaplusDB技术】读取列表指定位置数据接口说明——[List表]
What are the main mobile camera chips?
Codeforces Round #783 (Div. 2)
About the case of server URL
Some problems in the use of zuul
腾讯云域名绑定
Stm32f429bit6 SD card analog U disk
Esp32-cam usage history
redis setex和set 的区别
Case 4-1.7: file transfer (concurrent search)
字符串String的slice、replace、toUpperCase和repeat等方法会不会改变原有字符串
JS基础11
JS基础7
手机摄像头芯片主要有哪些?
论文阅读《Attention Concatenation Volume for Accurate and Efficient Stereo Matching》
What does 0ul or 1ul in STM32 mean?
低频(LF)RFID 智能终端
In recent years, oppo, Xiaomi and other mobile phone manufacturers have begun to take the road of self-developed chips. Can this road run through?
redis 不能添加数据