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>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
        }
        select {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
        }
        #loading, #error {
            text-align: center;
            font-style: italic;
            color: #666;
        }
        #error {
            color: #f00;
        }
    </style>
</head>
<body>
    <h1>省市区三级联动选择</h1>
    <div id="loading">正在加载数据...</div>
    <div id="error" style="display: none;"></div>
    <select id="province" style="display: none;"></select>
    <select id="city" style="display: none;"></select>
    <select id="district" style="display: none;"></select>

    <script>
        const provinceSelect = document.getElementById('province');
        const citySelect = document.getElementById('city');
        const districtSelect = document.getElementById('district');
        const loadingDiv = document.getElementById('loading');
        const errorDiv = document.getElementById('error');

        let data = [];

        async function fetchCSVData(url) {
            try {
                const response = await fetch(url);
                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }
                const csvText = await response.text();
                return csvText.split('\n').slice(1).filter(row => row.trim() !== '').map(row => {
                    const columns = row.split(',');
                    if (columns.length < 8) {
                        console.warn('Row with insufficient columns:', row);
                        return null;
                    }
                    const [id, pid, deep, name, pinyin_prefix, pinyin, ext_id, ext_name] = columns;
                    return {
                        id: parseInt(id) || 0,
                        pid: parseInt(pid) || 0,
                        deep: parseInt(deep) || 0,
                        name: (name || "").replace(/"/g, ''),
                        ext_name: (ext_name || "").replace(/"/g, '')
                    };
.........完整代码请登录后点击上方下载按钮下载查看

网友评论0