DIV CSS 佈局教程網

jQuery 實作 select 多選框的左右搬移
編輯:JavaScript綜合知識     

後台設定頁常見的「可選項目」與「已選項目」雙欄介面,本質上就是把 option 從左邊的 select 搬到右邊,再從右邊搬回左邊。用 jQuery 幾行就能完成,這裡附完整可執行範例。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>選擇分包</title>
<script type="text/javascript" src="jquery-1.4.4.min.js"></script>
<script type="text/javascript">
// 動態為左側清單建立選項(實務上可改為從資料庫讀取)
$(document).ready(function(){
    for (var i = 1; i <= 5; i++) {
        $("#fb_list").append("<option value='" + i + "'>公開招標小型機採購00" + i + "</option>");
    }
});

// 往右搬:把左側選取的項目移到右側
$(function(){
    $("#add").click(function(){
        if ($("#fb_list option:selected").length > 0) {
            $("#fb_list option:selected").each(function(){
                $("#select_list").append("<option value='" + $(this).val() + "'>" + $(this).text() + "</option>");
                $(this).remove();
            });
        } else {
            alert("請選擇要添加的分包!");
        }
    });
});

// 往左搬:把右側選取的項目移回左側
$(function(){
    $("#delete").click(function(){
        if ($("#select_list option:selected").length > 0) {
            $("#select_list option:selected").each(function(){
                $("#fb_list").append("<option value='" + $(this).val() + "'>" + $(this).text() + "</option>");
                $(this).remove();
            });
        } else {
            alert("請選擇要刪除的分包!");
        }
    });
});
</script>
</head>
<body>
<table width="95%" cellpadding="0" align="center" class="listshow" border="1" cellspacing="0">
<tr>
  <td colspan="4" align="center">選擇分包</td>
</tr>
<tr>
  <td class="black" width="30%" align="center" height="150">
    <select id="fb_list" multiple="multiple" style="text-align:center;width:300px;height:150px;"></select>
  </td>
  <td align="center" width="5%">
    <input type="button" id="add" value="添加>>" />
    <br/><br/>
    <input type="button" id="delete" value="<<刪除" />
  </td>
  <td class="black" width="30%" align="center">
    <select id="select_list" multiple="multiple" style="text-align:center;width:300px;height:150px;"></select>
  </td>
</tr>
</table>
</body>
</html>

運作原理

核心邏輯只有三步:先用 option:selected 確認有選 anything、再用 .each() 遍歷每個被選取的 option 把它的 value 與 text 複製到目標 select、最後把原 option 從 DOM 移除。因為是「複製再刪除」,效果上就是搬移。

select 必須加上 multiple="multiple" 才能多選,實務上通常還會搭配 size 屬性或直接以 CSS 固定高度,避免清單被壓成一列。

可以再改善的地方

用 clone 取代手動重建 option。$(this).clone() 會完整保留所有屬性(包括自訂的 data-* 或 disabled),比手動組字串安全,也不必擔心文字中含有引號會破壞 HTML。

加上全選與雙向批次搬移。多數使用者一次會搬好幾項,提供「全部添加/全部移除」按鈕能省下大量操作。

避免用 alert 提示。改在介面上顯示一行淡色提示文字,不中斷操作流。

記得送出時讀右側的值。表單提交時要把 #select_list 的選項收集起來(例如以 hidden input 陣列承載),否則後端收不到使用者真正選的內容。

HTML 中的 value 屬性有個小瑕疵。範例在 append 時漏了 option 的右引號(<option value='...'> 少了一個單引號),部分瀏覽器會容错處理,但嚴謹環境下會造成屬性解析錯誤,建議改用 clone 一併解決。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved