Skip to main content

select 穿梭

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery左右穿梭框</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f7fa;
            padding: 30px;
            color: #333;
        }
        
        .container {
            max-width: 900px;
            margin: 0 auto;
            background: white;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            padding: 25px;
        }
        
        h1 {
            text-align: center;
            margin-bottom: 25px;
            color: #2c3e50;
            font-weight: 600;
        }
        
        .description {
            text-align: center;
            margin-bottom: 30px;
            color: #7f8c8d;
            line-height: 1.6;
        }
        
        .transfer-box {
            display: flex;
            justify-content: space-between;
            align-items: stretch;
            gap: 20px;
        }
        
        .box {
            flex: 1;
            border: 1px solid #e1e4e8;
            border-radius: 8px;
            display: flex;
            flex-direction: column;
            overflow: hidden;
        }
        
        .box-header {
            background-color: #f8f9fa;
            padding: 12px 15px;
            border-bottom: 1px solid #e1e4e8;
            font-weight: 600;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .count {
            background-color: #3498db;
            color: white;
            border-radius: 12px;
            padding: 2px 10px;
            font-size: 0.8em;
        }
        
        .search-box {
            padding: 10px 15px;
            border-bottom: 1px solid #e1e4e8;
        }
        
        .search-box input {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }
        
        .select-container {
            flex: 1;
            overflow: auto;
            height: 300px;
        }
        
        select {
            width: 100%;
            height: 100%;
            border: none;
            padding: 5px;
            font-size: 14px;
            outline: none;
        }
        
        select option {
            padding: 8px 10px;
            border-bottom: 1px solid #f0f0f0;
            cursor: pointer;
        }
        
        select option:hover {
            background-color: #f0f7ff;
        }
        
        .buttons {
            display: flex;
            flex-direction: column;
            justify-content: center;
            gap: 15px;
            padding: 0 15px;
        }
        
        button {
            padding: 10px 15px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.3s;
            min-width: 100px;
        }
        
        button:hover {
            background-color: #2980b9;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        
        button:active {
            transform: translateY(0);
        }
        
        button:disabled {
            background-color: #bdc3c7;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }
        
        .footer {
            margin-top: 25px;
            text-align: center;
            color: #7f8c8d;
            font-size: 0.9em;
        }
        
        @media (max-width: 768px) {
            .transfer-box {
                flex-direction: column;
            }
            
            .buttons {
                flex-direction: row;
                justify-content: center;
                margin: 15px 0;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>jQuery左右穿梭框</h1>
        <p class="description">从左侧选择项目并移动到右侧,支持多选、全选和搜索功能</p>
        
        <div class="transfer-box">
            <!-- 左侧选择框 -->
            <div class="box">
                <div class="box-header">
                    <span>可选项目</span>
                    <span class="count" id="leftCount">0</span>
                </div>
                <div class="search-box">
                    <input type="text" id="leftSearch" placeholder="搜索...">
                </div>
                <div class="select-container">
                    <select multiple id="leftSelect" size="10">
                        <option value="1">JavaScript 编程</option>
                        <option value="2">Python 数据分析</option>
                        <option value="3">Java 开发</option>
                        <option value="4">React 前端框架</option>
                        <option value="5">Vue.js 框架</option>
                        <option value="6">Node.js 后端开发</option>
                        <option value="7">数据库设计</option>
                        <option value="8">移动应用开发</option>
                        <option value="9">UI/UX 设计</option>
                        <option value="10">项目管理</option>
                        <option value="11">人工智能</option>
                        <option value="12">机器学习</option>
                        <option value="13">大数据技术</option>
                        <option value="14">云计算</option>
                        <option value="15">网络安全</option>
                    </select>
                </div>
            </div>
            
            <!-- 操作按钮 -->
            <div class="buttons">
                <button id="addAll">全部添加 &gt;&gt;</button>
                <button id="addSelected">添加选中 &gt;</button>
                <button id="removeSelected">&lt; 移除选中</button>
                <button id="removeAll">&lt;&lt; 全部移除</button>
            </div>
            
            <!-- 右侧选择框 -->
            <div class="box">
                <div class="box-header">
                    <span>已选项目</span>
                    <span class="count" id="rightCount">0</span>
                </div>
                <div class="search-box">
                    <input type="text" id="rightSearch" placeholder="搜索...">
                </div>
                <div class="select-container">
                    <select multiple id="rightSelect" size="10">
                    </select>
                </div>
            </div>
        </div>
        
        <div class="footer">
            <p>提示:按住Ctrl键可多选,使用搜索框可快速过滤选项</p>
        </div>
    </div>

    <script>
        $(document).ready(function() {
            // 更新计数函数
            function updateCounts() {
                $('#leftCount').text($('#leftSelect option').length);
                $('#rightCount').text($('#rightSelect option').length);
            }
            
            // 初始化计数
            updateCounts();
            
            // 全部添加到右侧
            $('#addAll').click(function() {
                $('#leftSelect option').each(function() {
                    $(this).detach().appendTo('#rightSelect');
                });
                updateCounts();
            });
            
            // 添加选中项到右侧
            $('#addSelected').click(function() {
                $('#leftSelect option:selected').each(function() {
                    $(this).detach().appendTo('#rightSelect');
                });
                updateCounts();
            });
            
            // 从右侧移除选中项
            $('#removeSelected').click(function() {
                $('#rightSelect option:selected').each(function() {
                    $(this).detach().appendTo('#leftSelect');
                });
                updateCounts();
            });
            
            // 全部移除到左侧
            $('#removeAll').click(function() {
                $('#rightSelect option').each(function() {
                    $(this).detach().appendTo('#leftSelect');
                });
                updateCounts();
            });
            
            // 左侧搜索功能
            $('#leftSearch').on('input', function() {
                var filter = $(this).val().toLowerCase();
                $('#leftSelect option').each(function() {
                    var text = $(this).text().toLowerCase();
                    if (text.indexOf(filter) > -1) {
                        $(this).show();
                    } else {
                        $(this).hide();
                    }
                });
            });
            
            // 右侧搜索功能
            $('#rightSearch').on('input', function() {
                var filter = $(this).val().toLowerCase();
                $('#rightSelect option').each(function() {
                    var text = $(this).text().toLowerCase();
                    if (text.indexOf(filter) > -1) {
                        $(this).show();
                    } else {
                        $(this).hide();
                    }
                });
            });
            
            // 双击选项快速移动
            $('#leftSelect').on('dblclick', 'option', function() {
                $(this).detach().appendTo('#rightSelect');
                updateCounts();
            });
            
            $('#rightSelect').on('dblclick', 'option', function() {
                $(this).detach().appendTo('#leftSelect');
                updateCounts();
            });
        });
    </script>
</body>
</html>