很乱,慢慢看看
<script>
function selectAllOptions(obj) {
if (obj=="all"){
for (var i=0; i<document.form1.strUsername.options.length; i++) {
document.form1.strUsername.options[i].selected = true;
}
for (var i=0; i<document.form1.strEdit.options.length; i++) {
document.form1.strEdit.options[i].selected = true;
}

}else{
var iMaxCounter = obj.options.length;
for (var i=0; i<iMaxCounter; i++) {
obj.options[i].selected = true;
}
}
}
function moveSelectedOptions(from) {
var chkselect;
//if (document.form1.admintype[0].checked) {
// from=document.form1.strUsername
for (var i=0; i<from.options.length; i++) {
var o = from.options[i];
if (o.selected) {
chkselect=0;
for (var j=0; j<document.form1.strEdit.options.length; j++) {
if (document.form1.strEdit.options[j].value==o.value){chkselect=1;break;}
}
if (chkselect==0){
document.form1.strEdit.options[document.form1.strEdit.options.length] = new Option( o.text, o.value, true, true);
}
}
}
//}

/*if (document.form1.admintype[1].checked) {
from=document.form1.strAdd
for (var i=0; i<from.options.length; i++) {
var o = from.options[i];
if (o.selected) {
chkselect=0;
for (var j=0; j<document.form1.strEdit.options.length; j++) {
if (document.form1.strEdit.options[j].value==o.value){chkselect=1;break;}
}
if (chkselect==0){
document.form1.strEdit.options[document.form1.strEdit.options.length] = new Option( o.text, o.value, true, true);
}
}
}
}*/
//from.selectedIndex = -1;
}
function delSelectedOptions() {
for (var j=(document.form1.strEdit.options.length-1);j>=0; j--) {
if (document.form1.strEdit.options[j].selected){document.form1.strEdit.options[j]=null;}
}
}
function moveAllOptions(from) {
selectAllOptions(from);
moveSelectedOptions(from);
}
function delAllOptions() {
//if (document.form1.admintype[0].checked) {selectAllOptions(document.form1.strAdd);}
//if (document.form1.admintype[1].checked) {selectAllOptions(document.form1.strEdit);}
selectAllOptions(document.form1.strEdit);
delSelectedOptions();
}
</script>

解决方案 »

  1.   

    一个函数就够了<BODY>
    <FORM name="form1" method="post" action="">
    <SELECT style="width:80px" name="sel1" size="8" id="sel1">
    <OPTION>aaaa</OPTION>
    <OPTION>aaaa</OPTION>
    <OPTION>aaaa</OPTION>
    <OPTION>aaaa</OPTION>
    <OPTION>aaaa</OPTION>
    </SELECT>
    <SELECT style="width:80px" name="sel2" id="sel2" size="8">
    <OPTION>bbbb</OPTION>
    <OPTION>bbbb</OPTION>
    <OPTION>aaaa</OPTION>
    <OPTION>aaaa</OPTION>
    </SELECT>
    </FORM>
    <INPUT type="button" value="leftToRight" onClick="moveAll(document.all.sel1,document.all.sel2)">
    <INPUT type="button" value="rightToLeft" onClick="moveAll(document.all.sel2,document.all.sel1)">
    </BODY>
    <SCRIPT language="javascript">
    function moveAll(source, target)
    {
       var arrSourceValue = new Array();
       var arrSourceText = new Array();
       var arrTargetValue = new Array();
       var arrTargetText = new Array();
       var i;
       for (i = 0; i < target.options.length; i++)
       {
            arrTargetValue[i] = target.options[i].value;
            arrTargetText[i] = target.options[i].text;
       }
       for(i = 0; i < source.options.length; i++)
       {
          arrTargetValue[arrTargetValue.length] = source.options[i].value;
          arrTargetText[arrTargetText.length] = source.options[i].text;
       }
       source.length = 0;
       target.length = 0;
       for(i = 0; i < arrTargetValue.length; i++)
       {
          var no = new Option();
          no.value = arrTargetValue[i];
          no.text = arrTargetText[i];
          target[i] = no;
       }
    }
    </SCRIPT>