DIV CSS 佈局教程網

jQuery 操作 Select 的 Option:上下移動、刪除、新增與取值
編輯:JQuery特效代碼     

多選排序、授權範圍挑選這類介面,常需要讓使用者把下拉清單裡的項目調整順序或增刪。用 jQuery 處理 <option> 其實很直覺,關鍵是熟悉 insertBefore、insertAfter、prepend、append 這幾個插入方法的相對位置語意。

完整範例

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<script type="text/javascript" src="jquery-1.9.1.min.js"></script>

<script type="text/javascript">
/**
 * 向上移動選中的 option
 */
function upSelectedOption(){
    if(null == $('#where').val()){
        alert('請選擇一項');
        return false;
    }
    var optionIndex = $('#where').get(0).selectedIndex; // 選中的索引,從 0 開始
    if(optionIndex > 0){                                // 不在最上面才能往上
        $('#where option:selected').insertBefore($('#where option:selected').prev('option'));
    }
}

/**
 * 向下移動選中的 option
 */
function downSelectedOption(){
    if(null == $('#where').val()){
        alert('請選擇一項');
        return false;
    }
    var optionLength = $('#where')[0].options.length;   // 總長度,從 1 開始算
    var optionIndex  = $('#where').get(0).selectedIndex;
    if(optionIndex < (optionLength - 1)){               // 不在最下面才能往下
        $('#where option:selected').insertAfter($('#where option:selected').next('option'));
    }
}

/**
 * 移除選中的 option
 */
function removeSelectedOption(){
    if(null == $('#where').val()){
        alert('請選擇一項');
        return false;
    }
    $('#where option:selected').remove();
}

/**
 * 取得選中項與全部 option 的 id 順序
 */
function getSelectedOption(){
    var checkText  = $('#where').find('option:selected').text();
    var checkValue = $('#where').val();
    alert('當前被選中的 text=' + checkText + ', value=' + checkValue);
    var ids = '';
    var options = $('#where')[0].options;
    for(var i=0; i<options.length; i++){
        ids = ids + '`' + options[i].id;
    }
    alert('當前被選中的編號順序為' + ids);
}

/**
 * 新增 option
 */
function addSelectedOption(){
    $('#where').prepend('<option value="hbin" id="where06">Haerbin</option>');   // 插到第一位
    $('#where').append('<option value="hlj" id="where07">HeiLongJiang</option>'); // 插到最後一位
    $('#where').attr('size', 7);
}
</script>

<div id="updown">
<select id="where" name="where" size="5">
    <option value="hk" id="where01">Hong Kong</option>
    <option value="tw" id="where02">Taiwan</option>
    <option value="cn" id="where03">China</option>
    <option value="us" id="where04">United States</option>
    <option value="ca" id="where05">Canada</option>
</select>
</div>
<br/>
<input type="button" value="上移" onclick="upSelectedOption()"/>
<input type="button" value="下移" onclick="downSelectedOption()"/>
<input type="button" value="刪除" onclick="removeSelectedOption()"/>
<input type="button" value="確定" onclick="getSelectedOption()"/>
<input type="button" value="添加" onclick="addSelectedOption()"/>

原理與實務提醒

移動的關鍵在於「先抓參照點,再插入」。上移時以目前的上一個 option 為參照,呼叫 insertBefore;下移時以下一個 option 為參照,呼叫 insertAfter。由於 jQuery 會先把選中節點從原位置移開再插入,因此不會產生複本,這點和 HTML5 的原生 insertBefore 行為一致。

邊界判斷不能省。索引從 0 開始而長度從 1 開始,所以「還能往下」的條件是 optionIndex < optionLength - 1;寫錯就會出現選項莫名跑到另一頭的現象。

另外要提醒的是,用字串拼 HTML 來新增 option 雖然方便,但如果 option 的文字來自使用者輸入,就可能被注入標籤。這種情況建議改用 DOM 方式建立節點,再以 .text() 指定文字內容,例如 $('<option>').val(v).text(t).appendTo('#where'),安全性會好得多。

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