Posted in Javascript onFebruary 11, 2020
本文实例为大家分享了js实现省份城市的三级联动的具体代码,供大家参考,具体内容如下
效果图:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> select{ width: 80px; margin: 5px; } </style> <script> // 省份数组 var provinceArr = ['上海', '江苏', '河北']; // 城市数组 var cityArr = [ ['上海市'], ['苏州市', '南京市', '扬州市'], ['石家庄', '秦皇岛', '张家口'] ]; // 区域数组 var countryArr = [ [ ['黄浦区', '静安区', '长宁区', '浦东区'] ], [ ['虎丘区', '吴中区', '相城区', '姑苏区', '吴江区'], ['玄武区', '秦淮区', '建邺区', '鼓楼区', '浦口区'], ['邗江区', '广陵区', '江都区'] ], [ ['长安区', '桥西区', '新华区', '井陉矿区'], ['海港区', '山海关区', '北戴河区', '抚宁区'], ['桥东区', '桥西区', '宣化区', '下花园区'] ] ]; window.onload = function(){ var province = document.getElementById('province'); var city = document.getElementById('city'); var country = document.getElementById('country'); createOption(province, provinceArr); province.onchange = function(){ city.length = 0; createOption(city, cityArr[this.selectedIndex]); city.onchange(); } city.onchange = function(){ country.length = 0; createOption(country, countryArr[province.selectedIndex][this.selectedIndex]); } province.onchange(); } function createOption(obj, data){ for (var i = 0; i < data.length; i++) { var newOption = new Option(data[i], data[i]); obj.add(newOption, null); } } </script> </head> <body> 省份<select name="" id="province"></select> 城市<select name="" id="city"></select> 区域<select name="" id="country"></select> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。
JavaScript实现省份城市的三级联动
- Author -
da_yu_hai_tang声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@