<!DOCTYPE html>
<html>
<head>
    <title>员工信息管理</title>
    <style>
        .form-control {
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <h1>员工信息管理</h1>
<pre><code>&lt;!-- 添加员工表单 --&gt;
&lt;h2&gt;添加员工&lt;/h2&gt;
&lt;form id=&quot;addForm&quot;&gt;
    &lt;div&gt;
        &lt;label for=&quot;name&quot;&gt;姓名:&lt;/label&gt;
        &lt;input type=&quot;text&quot; id=&quot;name&quot; class=&quot;form-control&quot; required&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;label for=&quot;gender&quot;&gt;性别:&lt;/label&gt;
        &lt;select id=&quot;gender&quot; class=&quot;form-control&quot; required&gt;
            &lt;option value=&quot;男&quot;&gt;男&lt;/option&gt;
            &lt;option value=&quot;女&quot;&gt;女&lt;/option&gt;
        &lt;/select&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;label for=&quot;birthday&quot;&gt;出生日期:&lt;/label&gt;
        &lt;input type=&quot;date&quot; id=&quot;birthday&quot; class=&quot;form-control&quot; required&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;label for=&quot;address&quot;&gt;地址:&lt;/label&gt;
        &lt;select id=&quot;province&quot; class=&quot;form-control&quot; required&gt;
            &lt;option value=&quot;&quot;&gt;请选择省&lt;/option&gt;
            &lt;!-- 省级选项 --&gt;
        &lt;/select&gt;
        &lt;select id=&quot;city&quot; class=&quot;form-control&quot; required&gt;
            &lt;option value=&quot;&quot;&gt;请选择市&lt;/option&gt;
            &lt;!-- 市级选项 --&gt;
        &lt;/select&gt;
        &lt;select id=&quot;area&quot; class=&quot;form-control&quot; required&gt;
            &lt;option value=&quot;&quot;&gt;请选择区&lt;/option&gt;
            &lt;!-- 区级选项 --&gt;
        &lt;/select&gt;
        &lt;input type=&quot;text&quot; id=&quot;address&quot; class=&quot;form-control&quot; required&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;label for=&quot;phone&quot;&gt;联系电话:&lt;/label&gt;
        &lt;input type=&quot;text&quot; id=&quot;phone&quot; class=&quot;form-control&quot; required&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;button type=&quot;submit&quot;&gt;添加&lt;/button&gt;
    &lt;/div&gt;
&lt;/form&gt;

&lt;!-- 员工列表 --&gt;
&lt;h2&gt;员工列表&lt;/h2&gt;
&lt;table id=&quot;employeeTable&quot;&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;姓名&lt;/th&gt;
            &lt;th&gt;性别&lt;/th&gt;
            &lt;th&gt;出生日期&lt;/th&gt;
            &lt;th&gt;地址&lt;/th&gt;
            &lt;th&gt;联系电话&lt;/th&gt;
            &lt;th&gt;操作&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;!-- 员工信息行 --&gt;
    &lt;/tbody&gt;
&lt;/table&gt;

&lt;script&gt;
    // 获取省级选项
    fetch('pca_provinces.json')
        .then(response =&gt; response.json())
        .then(provinces =&gt; {
            const provinceSelect = document.getElementById('province');
            provinces.forEach(province =&gt; {
                const option = document.createElement('option');
                option.value = province.name;
                option.textContent = province.name;
                provinceSelect.appendChild(option);
            });
        });
    
    // 根据选择的省级选项获取市级选项
    document.getElementById('province').addEventListener('change', function() {
        const provinceName = this.value;
        const citySelect = document.getElementById('city');
        
        // 清空已有的市、区级选项
        citySelect.innerHTML = '&lt;option value=&quot;&quot;&gt;请选择市&lt;/option&gt;';
        document.getElementById('area').innerHTML = '&lt;option value=&quot;&quot;&gt;请选择区&lt;/option&gt;';
        
        if (provinceName) {
            fetch('pca_cities.json')
                .then(response =&gt; response.json())
                .then(cities =&gt; {
                    const filteredCities = cities.filter(city =&gt; city.provinceName === provinceName);
                    filteredCities.forEach(city =&gt; {
                        const option = document.createElement('option');
                        option.value = city.name;
                        option.textContent = city.name;
                        citySelect.appendChild(option);
                    });
                });
        }
    });
    
    // 根据选择的市级选项获取区级选项
    document.getElementById('city').addEventListener('change', function() {
        const cityName = this.value;
        const areaSelect = document.getElementById('area');
        
        // 清空已有的区级选项
        areaSelect.innerHTML = '&lt;option value=&quot;&quot;&gt;请选择区&lt;/option&gt;';
        
        if (cityName) {
            fetch('pca_areas.json')
                .then(response =&gt; response.json())
                .then(areas =&gt; {
                    const filteredAreas = areas.filter(area =&gt; area.cityName === cityName);
                    filteredAreas.forEach(area =&gt; {
                        const option = document.createElement('option');
                        option.value = area.name;
                        option.textContent = area.name;
                        areaSelect.appendChild(option);
                    });
                });
        }
    });
    
    // 添加员工
    document.getElementById('addForm').addEventListener('submit', function(event) {
        event.preventDefault();
        
        const name = document.getElementById('name').value;
        const gender = document.getElementById('gender').value;
        const birthday = document.getElementById('birthday').value;
        const province = document.getElementById('province').value;
        const city = document.getElementById('city').value;
        const area = document.getElementById('area').value;
        const address = document.getElementById('address').value;
        const phone = document.getElementById('phone').value;
        
        // 拼接地址
        const fullAddress = province + city + area + address;
        
        // 校验年龄
        const today = new Date();
        const birthDate = new Date(birthday);
        const age = today.getFullYear() - birthDate.getFullYear();
        const monthDiff = today.getMonth() - birthDate.getMonth();
        if (monthDiff &lt; 0 || (monthDiff === 0 &amp;&amp; today.getDate() &lt; birthDate.getDate())) {
            age--;
        }
        if (age &lt; 18) {
            alert('出生年月不得小于18周岁!');
            return;
        }
        
        // 校验联系电话
        const existingPhones = Array.from(document.querySelectorAll('#employeeTable tbody tr td:nth-child(5)')).map(td =&gt; td.textContent);
        if (existingPhones.includes(phone)) {
            alert('联系电话重复!');
            return;
        }
        
        // 创建员工信息行
        const row = document.createElement('tr');
        const nameCell = document.createElement('td');
        const genderCell = document.createElement('td');
        const birthdayCell = document.createElement('td');
        const addressCell = document.createElement('td');
        const phoneCell = document.createElement('td');
        const deleteButtonCell = document.createElement('td');
        
        nameCell.textContent = name;
        genderCell.textContent = gender;
        birthdayCell.textContent = birthday;
        addressCell.textContent = fullAddress;
        phoneCell.textContent = phone;
        deleteButtonCell.innerHTML = '&lt;button class=&quot;deleteButton&quot;&gt;删除&lt;/button&gt;';
        
        row.appendChild(nameCell);
        row.appendChild(genderCell);
        row.appendChild(birthdayCell);
        row.appendChild(addressCell);
        row.appendChild(phoneCell);
        row.appendChild(deleteButtonCell);
        
        document.getElementById('employeeTable').getElementsByTagName('tbody')[0].appendChild(row);
        
        // 清空表单
        document.getElementById('name').value = '';
        document.getElementById('gender').value = '男';
        document.getElementById('birthday').value = '';
        document.getElementById('province').value = '';
        document.getElementById('city').value = '';
        document.getElementById('area').value = '';
        document.getElementById('address').value = '';
        document.getElementById('phone').value = '';
    });
    
    // 删除员工
    document.getElementById('employeeTable').addEventListener('click', function(event) {
        if (event.target.classList.contains('deleteButton')) {
            event.target.closest('tr').remove();
        }
    });
&lt;/script&gt;
</code></pre>
</body>
</html>
员工信息管理系统 - 添加、查询、删除员工

原文地址: https://www.cveoy.top/t/topic/8ot 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录