下面给出源码,已通过火狐的测试,但是有个问题,如何限定List2中的个数,如超过5个则弹出提示信息或不执行.
===============================================================
<script type=text/javascript>
//中国机械加工网http://www.cnjiagong.com
//三维全景网http://www.feyou.com
//嘀咕网http://www.digood.com
<!--
function add (listTo, listFrom) {
    var moved = new Array();
    var count = 0;
    for (var item = 0; item < listFrom.options.length; item++) {
        if (listFrom.options[item].selected) {
            var temp = document.createElement("OPTION");
            temp.text = listFrom.options[item].text;
            temp.value = listFrom.options[item].value;
            var index = 0;
            var currOpn;
            while (index < listTo.options.length && temp.text > listTo.options[index].text) {
                index ++;
            }
            if (index < listTo.options.length) {
                currOpn = listTo.options[index];
            }
            else {
                currOpn = null;
            }
            try {
                listTo.add(temp, currOpn);
            }
            catch(ex) {
                listTo.add(temp, index);
            }
            moved[count] = listFrom.options[item].value;
            count ++;
        }
    }
    if (moved.length > 0) {
        remove(listFrom, moved);
    }
}function remove (listToRemoveFrom, items) {
    for (element in items) {        var index = 0;
        while (index < listToRemoveFrom.options.length &&
                listToRemoveFrom.options[index].value != items[element]) {
            index ++;
        }
        listToRemoveFrom.remove(index);
    }
}
-->
</script>
<form method="post">
<table style="text-align: center" border="0">
<tr><td><strong>List 1:</strong><br/>
<select size="15" name="list1[]" id="list1" style="width: 350px" multiple="multiple">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
<option value="5">Option 5</option>
<option value="6">Option 6</option>
</select></td>
<td><br/>
<input type="button" value="加入>>" onClick="add(this.form.list2, this.form.list1)"/>
<br/><br/>
<input type="button" value="<<删除" onClick="add(this.form.list1, this.form.list2)"/>
</td><td><strong>List 2:</strong><br/>
<select size="15" name="list2[]" id="list2" style="width: 350px" multiple="multiple">
</select></td></tr>
</table>
</form><br>
您最多只能选择5项!

解决方案 »

  1.   

    你可以在list2的加入元素的时候判断列表的个数啊,如果已经是5个就弹出提示
      

  2.   

    我对JAVASCRIPT不懂,能不能给个代码?
      

  3.   

    在函数中你取得第二个下拉框的option的个数,然后判断是否大于5就可以了,
    var length = document.form的名字.list2[].size;
    if(parseInt(length) > 5){}
      

  4.   

    <script type=text/javascript>
    //中国机械加工网http://www.cnjiagong.com
    //三维全景网http://www.feyou.com
    //嘀咕网http://www.digood.com
    <!--
    function add (listTo, listFrom) {
        var moved = new Array();
        var count = 0;
        for (var item = 0; item <5; item++) {
            if (listFrom.options[item].selected) {
                var temp = document.createElement("OPTION");
                temp.text = listFrom.options[item].text;
                temp.value = listFrom.options[item].value;
                var index = 0;
                var currOpn;
                while (index < listTo.options.length && temp.text > listTo.options[index].text) {
                    index ++;
                }
                if (index < listTo.options.length) {
                    currOpn = listTo.options[index];
                }
                else {
                    currOpn = null;
                }
                try {
                    listTo.add(temp, currOpn);
                }
                catch(ex) {
                    listTo.add(temp, index);
                }
                moved[count] = listFrom.options[item].value;
                count ++;
            }
        }
        if (moved.length > 0) {
            remove(listFrom, moved);
        }
    }function remove (listToRemoveFrom, items) {
        for (element in items) {        var index = 0;
            while (index < listToRemoveFrom.options.length &&
                    listToRemoveFrom.options[index].value != items[element]) {
                index ++;
            }
            listToRemoveFrom.remove(index);
        }
    }
    -->
    </script>