js实现最新2024省市区街道数据四级联动下拉选择效果代码

代码语言:html

所属分类:其他

代码描述:js实现最新2024省市区街道数据四级联动下拉选择效果代码,采用fetch动态请求cvs数据文件。

代码标签: js 最新 2024 省市区 街道 数据 四级 联动 下拉 选择

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>四级联动选择</title>
    <style>
        select {
            margin: 10px;
            padding: 5px;
            font-size: 16px;
        }
    </style>
</head>
<body>
    <select id="province"></select>
    <select id="city"></select>
    <select id="district"></select>
    <select id="street"></select>

    <script>
        const csvUrl = '//repo.bfw.wiki/bfwrepo/csv/city_data_level4.csv'; // 替换为实际的CSV文件URL
        let data = [];

        const provinceSelect = document.getElementById('province');
        const citySelect = document.getElementById('city');
        const districtSelect = document.getElementById('district');
        const streetSelect = document.getElementById('street');

        function parseCSV(text) {
            const lines = text.split('\n').filter(line => line.trim() !== '');
            const headers = lines[0].split(',').map(header => header.trim().replace(/['"]+/g, ''));
            return lines.slice(1).map(line => {
                const values = line.split(',').map(value => value.trim().replace(/['"]+/g, ''));
                return headers.reduce((obj, header, index) => {
                    obj[header] = values[index] || '';
                    return obj;
                }, {});
            }).filter(obj => obj.id && obj.pid && obj.name); // 确保关键字段存在
        }

        function populateSelect(select, options) {
            select.innerHTML = '.........完整代码请登录后点击上方下载按钮下载查看

网友评论0