<script>
function AddSelection()
{
    var selectArray = new Array();
var selectArray_text = new Array();
    var selectArray_submit = new Array();
var selectArray_text_submit = new Array();
var tag = true;

//////////////////////////////////////////////////////
//
//将select1中选中的项存在selectArray数组中
//
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
for (var sa = 0; sa < document.form1.select1.length; sa++)
{
    //alert('sa='+sa);
    if (document.form1.select1[sa].selected)
{
    selectArray.push(document.form1.select1[sa].value);
selectArray_text.push(document.form1.select1[sa].text);
}
}

///////////////////////////////////////////
//
//取出准备提交select中的选项
//
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
for (var sa = 0; sa < document.form1.select2.length; sa++)
{
selectArray_submit.push(document.form1.select2[sa].value);
selectArray_text_submit.push(document.form1.select2[sa].text);
} //////////////////////////////////////////////////////
//
//将selectArray中选中的,而且不在selectArray_submit中的数据插入selectArray_submit中
//该循环最终结果是:selectArray_submit中保存用户选择的所有项,且不重复
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    for (var vi = 0; vi < selectArray.length; vi++)
{
    tag = true;
    for (var vb = 0; vb < selectArray_submit.length; vb++)
{
    if (selectArray[vi]==selectArray_submit[vb])
{
    tag = false;
}
}
if (tag)
{
//document.form1.s2.options[selectArray_submit.length+1]=new Option(selectArray[vi],selectArray_text[vi]);
selectArray_submit.push(selectArray[vi]);
selectArray_text_submit.push(selectArray_text[vi]);
}
}
//////////////////////////////////////////////////////
//
//在selectArray_submit中生成所有的用户选择过的项
//
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
for (var vc = 0; vc < selectArray_submit.length; vc++)
{
    document.form1.s2.options[vc]=new Option(selectArray_text_submit[vc],selectArray_submit[vc]);
}

//////////////////////////////////////////////////////
//
//测试selectArray中的数据是否正确
//
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
for (var t_sa = 0; t_sa< selectArray.length; t_sa++)
{
//alert('测试selectArray'+selectArray[t_sa]+'-----'+selectArray_text[t_sa]);
}

//////////////////////////////////////////////////////
//
//测试selectArray_submit中的数据是否正确
//
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
for (var t_sa = 0; t_sa< selectArray_submit.length; t_sa++)
{
//alert('准备提交:'+selectArray_submit[t_sa]+'-----'+selectArray_text_submit[t_sa]);
}
} //AddSelection()结束function del(){
    for (var sa = 0; sa < document.form1.select2.length; sa++)
{
    if (document.form1.select2[sa].selected)
{
document.form1.select2.options[sa]=null; //去掉一条
}
}}
</script>

解决方案 »

  1.   

    关键是这条:document.form1.select2.options[sa]=null; //去掉一条建议看看:《javascript权威指南》很好的一本书
      

  2.   

    var ss=document.getbyclientId("listbox1")
    ==>
    var ss=document.getElementById("ListBox1")<%@ Page language="c#" Codebehind="w3.aspx.cs" AutoEventWireup="false" Inherits="stuinfo.w3" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>w3</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    <script>
     function f1()
     {
     
          var ss=document.getElementById("ListBox1")
           ss.options.remove(ss.selectedIndex);  }
    </script>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:ListBox id="ListBox1" style="Z-INDEX: 101; LEFT: 352px; POSITION: absolute; TOP: 160px"
    runat="server" Width="104px" SelectionMode="Multiple">
    <asp:ListItem Value="aaaa">aaaa</asp:ListItem>
    <asp:ListItem Value="bbbb">bbbb</asp:ListItem>
    <asp:ListItem Value="cccc">cccc</asp:ListItem>
    <asp:ListItem Value="dddd">dddd</asp:ListItem>
    </asp:ListBox><INPUT style="Z-INDEX: 102; LEFT: 520px; WIDTH: 112px; POSITION: absolute; TOP: 184px; HEIGHT: 24px"
    type="button" value="Button" onclick="f1()">
    </form>
    </body>
    </HTML>