<script language="javascript">
function RemoveItem(ControlName)
    { 
Control = null;
switch (ControlName){
 case "btnReceSendToLeft" : 
   Control=eval("document.SelectReceiver.listSendTo");  
   break;
  } 

var j=Control.length;
if(j==0) return;
for(j;j>0;j--)
{
if(Control.options[j-1].selected==true)

  Control.remove(j-1);
}
}
   
} function AddItem(ControlName)
{
Control = null;
switch (ControlName){
 case "btnReceSendToRight" : 
   Control=eval("document.SelectReceiver.listSendTo");  
   break;


var i=0;
listAccount=eval("document.SelectReceiver.listAccount");
var j=listAccount.length;
for(i=0;i<j;i++)
{
if(listAccount.options[i].selected==true)

     
Control.add(new Option(listAccount[i].text,listAccount.options[i].value));          
}
}

} function setStatusright()
{
document.SelectReceiver.btnReceSendToRight.disabled = false;
} function setStatusleft()
{
document.SelectReceiver.btnReceSendToLeft .disabled =false;
}

function PopulateData()
{
   if (window.dialogArguments != null) 
{
var parwin = window.dialogArguments;
if(parwin.document.all.hdnTxtSendTo.value!="")
{
    Control=eval("document.SelectReceiver.listSendTo"); 
   var SendToValueArray = parwin.document.all.hdnTxtSendTo.value.split(",");
var SendToTxtArray = parwin.document.all.txtSendTo.value.split(",");
for(i=0;i<SendToValueArray.length-1;i++)
{
Control.add(new Option(SendToTxtArray[i],SendToValueArray[i]));
}
}


}
}
</script>
<table>
               <TR>
<TD align="right" width="45%">
<asp:dropdownlist id="listAccount" ondblclick="AddItem('btnReceSendToRight')" runat="server"  onchange="setStatusright()" multiple></asp:dropdownlist></TD>
<button id="AllSelect" type="button"><font face="webdings">8</font></button> <td "> <button onclick="AddItem(this.name)" name="btnReceSendToRight" type="button"><font face="webdings">4</font></button>
<br>
<button onclick="RemoveItem(this.name)" type="button" name="btnReceSendToLeft"><font face="webdings">3</font></button>
                 <br>
<button id="AllDel" type="button"><font face="webdings">7</font></button>
</td> <TD align="left" width="45%"><FONT face="宋体">
<SELECT id="listSendTo"  multiple size="21" name="listSendTo"></SELECT></FONT></TD>
</TR></table>上面的代码只是实现把一个选择项选到select中,我想实现点击一下按钮AllSelect就把dropdownlist中所有的项都选到select中。点击一下按钮AllDel就把select中所有的项都移除。该怎么实现?

解决方案 »

  1.   

    http://lucky.myrice.com/javascriptexam/add_del_Select.htm
      

  2.   

    例子function add()
    { oS1 = document.frm.s1
    oS2 = document.frm.s2
    for(i = oS1.options.length - 1;i>-1;i--)
    {
    if(oS1.options[i].selected)
    {
    oS2.options[oS2.options.length] = new Option(oS1.options[i].text,"")
    oS1.options[i].parentNode.removeChild(oS1.options[i])
    }
    }
    }
    function addAll()
    {
    oS1 = document.frm.s1
    oS2 = document.frm.s2
    for(i = oS1.options.length - 1;i>-1;i--)
    {
    oS2.options[oS2.options.length] = new Option(oS1.options[i].text,"")
    oS1.options[i].parentNode.removeChild(oS1.options[i])
    }
    }
    function remove()
    {
    oS1 = document.frm.s1
    oS2 = document.frm.s2
    for(i = oS2.options.length - 1;i>-1;i--)
    {
    if(oS2.options[i].selected)
    {
    oS1.options[oS1.options.length] = new Option(oS2.options[i].text,"")
    oS2.options[i].parentNode.removeChild(oS2.options[i])
    }
    }
    }
    function removeAll()
    {
    oS1 = document.frm.s1
    oS2 = document.frm.s2
    for(i = oS2.options.length - 1;i>-1;i--)
    {
    oS1.options[oS1.options.length] = new Option(oS2.options[i].text,"")
    oS2.options[i].parentNode.removeChild(oS2.options[i])
    }
    }