DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> JavaScript控制兩個列表框listbox左右交換數據的方法
JavaScript控制兩個列表框listbox左右交換數據的方法
編輯:關於JavaScript     

本文實例講述了JavaScript控制兩個列表框listbox左右交換數據的方法。分享給大家供大家參考。具體分析如下:

這個功能我們經常用到,將左邊列表框的元素移動到右邊,或者將右邊列表框的元素移動到左邊,可以一次性全部移動
復制代碼 代碼如下:function listbox_moveacross(sourceID, destID) {
    var src = document.getElementById(sourceID);
    var dest = document.getElementById(destID);
    for(var count=0; count < src.options.length; count++) {
        if(src.options[count].selected == true) {
                var option = src.options[count];
                var newOption = document.createElement("option");
                newOption.value = option.value;
                newOption.text = option.text;
                newOption.selected = true;
                try {
                         dest.add(newOption, null); //Standard
                         src.remove(count, null);
                 }catch(error) {
                         dest.add(newOption); // IE only
                         src.remove(count);
                 }
                count--;
        }
    }
}
//..
listbox_moveacross('countryList', 'selectedCountryList');
下面是像是的演示效果代碼,可以直接在浏覽器內執行
復制代碼 代碼如下:Click below buttons to move selected options right or left.<br>
<table>
<tbody><tr>
    <td>
<select id="sourceSelect" size="10" multiple="">
 <option value="a">Afghanistan</option>
 <option value="b">Bahamas</option>
 <option value="c">Barbados</option>
 <option value="d">Belgium</option>
 <option value="e">Bhutan</option>
 <option value="f">China</option>
 <option value="g">Croatia</option>
 <option value="h">Denmark</option>
 <option value="i">France</option>
</select>
    </td>
    <td>
        <button onclick="listboxMoveacross('sourceSelect', 'destSelect');">>></button>  <br>
        <button onclick="listboxMoveacross('destSelect', 'sourceSelect');"><<</button>
    </td>
    <td>
<select id="destSelect" size="10" multiple="">
 <option value="a">Afghanistan</option>
 <option value="b">Bahamas</option>
 <option value="c">Barbados</option>
 <option value="d">Belgium</option>
 <option value="e">Bhutan</option>
 <option value="f">China</option>
 <option value="g">Croatia</option>
 <option value="h">Denmark</option>
 <option value="i">France</option>
</select>
    </td>
</tr>
</tbody></table>
    <script>
function listboxMoveacross(sourceID, destID) {
    var src = document.getElementById(sourceID);
    var dest = document.getElementById(destID);
    for(var count=0; count < src.options.length; count++) {
        if(src.options[count].selected == true) {
                var option = src.options[count];
                var newOption = document.createElement("option");
                newOption.value = option.value;
                newOption.text = option.text;
                newOption.selected = true;
                try {
                         dest.add(newOption, null); //Standard
                         src.remove(count, null);
                 }catch(error) {
                         dest.add(newOption); // IE only
                         src.remove(count);
                 }
                count--;
        }
    }
}
</script>

希望本文所述對大家的javascript程序設計有所幫助。

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