jQuery实现一个下拉框多选功能(可删除,可搜索...

<!DOCTYPE html>
<html lang="zh">
<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;
            background-color: #f0f0f0;
            padding: 20px;
        }
        .dropdown {
            position: relative;
            display: inline-block;
            width: 300px; /* 设置宽度 */
        }
        .input-container {
            display: flex;
            flex-wrap: wrap;
            border: 1px solid #ccc;
            border-radius: 4px;
            padding: 5px;
            cursor: pointer;
            background-color: #fff;
            min-height: 40px; /* 固定高度 */
        }
        .tag {
            display: inline-block;
            background-color: #007bff; /* 标签背景色 */
            color: white; /* 标签字体颜色 */
            padding: 5px 10px;
            border-radius: 4px;
            margin: 5px 5px 0 0;
            position: relative;
        }
        .remove-btn {
            background-color: transparent;
            border: none;
            color: white;
            cursor: pointer;
            margin-left: 5px;
            font-weight: bold;
        }
        .dropdown-content {
            display: none;
            position: absolute;
            background-color: #fff;
            min-width: 100%;
            box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2);
            z-index: 1;
            border-radius: 4px; /* 圆角 */
            overflow: hidden;
        }
        .dropdown-content input {
            width: calc(100% - 16px);
            box-sizing: border-box;
            padding: 8px;
            margin: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        .department-item {
            padding: 8px 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .department-item:hover {
            background-color: #f1f1f1; /* 悬停效果 */
        }
    </style>
</head>
<body>

<div class="dropdown">
    <div class="input-container" id="dropdownButton" placeholder="选择部门">
        <!-- 选中的姓名标签将放在这里 -->
    </div>
    <div class="dropdown-content" id="dropdownContent">
        <input type="text" id="searchInput" placeholder="搜索部门..." />
        <div id="departmentList">
            <!-- 部门列表 -->
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        const departments = [
            { name: "张三", department: "人事部", phone: "12345678901", type: "员工" },
            { name: "李四", department: "财务部", phone: "12345678902", type: "承包商" },
            { name: "王五", department: "技术部", phone: "12345678903", type: "员工" },
            { name: "赵六", department: "市场部", phone: "12345678904", type: "员工" },
            { name: "钱七", department: "运营部", phone: "12345678905", type: "承包商" }
        ];
        let selectedDepartments = [];

        // 填充部门列表
        departments.forEach(item => {
            $('#departmentList').append(`
                <div class="department-item" data-name="${item.name}" data-department="${item.department}" data-phone="${item.phone}" data-type="${item.type}">
                    ${item.name} - ${item.department} - ${item.phone} - ${item.type}
                </div>
            `);
        });

        // 点击输入框时显示下拉框
        $('#dropdownButton').click(function() {
            $('#dropdownContent').toggle();
            $('#searchInput').val(''); // 清空搜索框
            $('#departmentList .department-item').show(); // 显示所有部门
        });

        // 搜索功能
        $('#searchInput').on('keyup', function() {
            const value = $(this).val().toLowerCase();
            $('#departmentList .department-item').filter(function() {
                $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1);
            });
        });

        // 处理部门选择
        $(document).on('click', '.department-item', function() {
            const name = $(this).data('name');

            const index = selectedDepartments.indexOf(name);
            if (index === -1) {
                selectedDepartments.push(name);
                $(this).addClass('selected'); // 添加高亮
            } else {
                selectedDepartments.splice(index, 1);
                $(this).removeClass('selected'); // 移除高亮
            }
            updateSelectedDepartments();
        });

        // 更新输入框内容
        function updateSelectedDepartments() {
            $('#dropdownButton').empty(); // 清空当前内容
            selectedDepartments.forEach(name => {
                $('#dropdownButton').append(`
                    <div class="tag">${name}<button class="remove-btn">×</button></div>
                `);
            });
        }

        // 删除选中的部门
        $(document).on('click', '.remove-btn', function(event) {
            const name = $(this).parent().text().slice(0, -1); // 获取姓名
            selectedDepartments = selectedDepartments.filter(dep => dep !== name);
            updateSelectedDepartments();
            event.stopPropagation(); // 阻止事件冒泡
        });

        // 点击外部关闭下拉框
        $(document).click(function(event) {
            if (!$(event.target).closest('.dropdown').length) {
                $('#dropdownContent').hide();
            }
        });
    });
</script>

</body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容