Javascript操作兩個select

來源:互聯網
上載者:User

前兩天要寫一個操作select的東西,因為急著交貨,隨便寫了一個操作LISTBOX的東西交上去了,
所有事件都是寫在伺服器端,覺得不是很好,今天重寫了一個,是伺服器端和用戶端互動的

 

前端只有一個資料繫結:

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SelectBind();
            }
        }

        protected void SelectBind()
        {
            for (int i = 0; i < 20; i++)
                selectleft.Items.Add(new ListItem("Andy"+(i+1).ToString(),(i+1).ToString()));
        }

 

 

源:

<body>
<script type="text/javascript" language="javascript">
    //交換全部,參數為要交換的對象
    function TranAllOption(Obj1,Obj2)
    {
        if(Obj1.options.length <=0)
            { alert("沒有要移動的項!"); return;}
        while(Obj1.options.length >0)
        {
            Obj2.options.add(new Option(Obj1.options[0].text,Obj1.options[0].value));
            Obj1.remove(0);
        }
    }
    //交換部份,參數為要交換的對象
    function TranOption(Obj1,Obj2)
    {  
        var show = true;
        for(var i=0;i<Obj1.options.length;i++)
        {
            if(Obj1.options[i].selected == true)
            {
                Obj2.options.add(new Option(Obj1.options[i].text,Obj1.options[i].value));
                Obj1.remove(i);
                i=i-1;
                show = false;
            }
        }
        if(show)
        {
            alert("請選擇要移動的項!");
        }
    }
    //上下移動 
    function SwapOption(obj,index)
    {
        if(obj.selectedIndex < 0)
        {
            alert("請選擇要移動的項!");
            return;
        }
        if(index == -1)
        {
            if (obj.selectedIndex == 0)
            {
                alert("已經移動到頂了!");
                return;
            }
            else { obj.options(obj.selectedIndex).swapNode(obj.options(obj.selectedIndex-1)); return;}
        }
        else if(index == 1)
        {
            if (obj.selectedIndex == obj.options.length-1)
            {
                alert("已經移動到底了!"); return;
            }
            else { obj.options(obj.selectedIndex).swapNode(obj.options(obj.selectedIndex+1)); return; }
        }
    }
</script>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
    <div>
        <table>
            <tr><td rowspan="4"><select runat="server" multiple="true" id="selectleft" style="border-style:solid; width:200px; height:300px;" ></select></td>
            <td><input id="Button1" type="button" value=">> 全部"  onclick="TranAllOption(selectleft,selectright)" /></td>
            <td rowspan="4"><select runat="server" multiple="true" id="selectright" style="border-style:solid; width:200px; height:300px;" ></select></td>
            <td rowspan="2"><input id="Button5" type="button" value="上移" onclick="SwapOption(selectright,-1);" /></td></tr>
            <tr><td><input id="Button2" type="button" value=">  增加" onclick="TranOption(selectleft,selectright)" /></td></tr>
            <tr><td><input id="Button3" type="button" value="<< 全部" onclick="TranAllOption(selectright,selectleft)" /></td>
            <td rowspan="2"><input id="Button6" type="button" value="下移" onclick="SwapOption(selectright,1)" /></td></tr>
            <tr><td><input id="Button4" type="button" value="<  增加" onclick="TranOption(selectright,selectleft)" /></td></tr>
        </table>
    </div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.