簡體   English   中英

在列表框之間移動項目

[英]Moving items between Listboxes

我的頁面上有兩個列表框,我需要借助javascript在它們之間移動項目。 這是我的標記:

<asp:ListBox ID="ListBox1" Height="300px" runat="server" AppendDataBoundItems="true" 
        SelectionMode="Multiple"></asp:ListBox>

<div>

<asp:ImageButton ID="ButtonRight" runat="server" ImageUrl="~/Images/right.gif" OnClientClick="return     
      LeftToRightMoveItems('AddSetup');" /><br />
<br />
<asp:ImageButton ID="ButtonLeft" runat="server" ImageUrl="~/Images/left.gif" OnClientClick="return 
       RightToLeftMoveItems('AddSetup');" />
</div>

<asp:ListBox ID="ListBox2" Height="300px" runat="server" AppendDataBoundItems="true"  
      SelectionMode="Multiple"></asp:ListBox>

這是我的JavaScript代碼:

Javascript:

 function LeftToRightMoveItems() {
        try {
            if (status == "AddSetup") {
                var varFromBox = document.getElementById("<%=ListBox1.ClientID%>").options;
                var varToBox = document.getElementById("<%=ListBox2.ClientID%>").options;
            }

            alert(varFromBox.length);
            alert(varToBox.length);

            if ((varFromBox != null)) {
                if (varFromBox.length < 1) {
                    alert('There are no items to move!');
                    return false;
                }
                if (varFromBox.options.selectedIndex == -1) // when no Item is selected the index will be -1
                {
                    alert('Please select an item to move!');
                    return false;
                }
                while (varFromBox.options.selectedIndex >= 0) {
                    var newOption = new Option(); // Create a new instance of ListItem 
                    newOption.text = varFromBox.options[varFromBox.options.selectedIndex].text;
                    newOption.value = varFromBox.options[varFromBox.options.selectedIndex].value;
                    varToBox.options[varToBox.length] = newOption; //Append the item in Target Listbox
                    varFromBox.remove(varFromBox.options.selectedIndex); //Remove the item from Source Listbox
                }
            }
        }
        catch (e) {
            alert("Following error occured : \n" + e.description);
        }
        return false;
    }

在頁面加載時,我正在ListBox1填充項目。 但是在alert()我得到了0個項目。

HTML看起來像:

<SELECT style="HEIGHT: 300px" id=ListBox1 multiple size=4 name=ctl00$ContentPlaceHolderNewSys$TabContainerMain$tabPanelAdd$tabContainerInnerAdd$tabPanelAdd_1$ListBoxAll1> <OPTION value=1>param1</OPTION> <OPTION value=2>param2</OPTION> <OPTION value=3>param3</OPTION></SELECT> 

該函數不接受狀態參數。 修改函數的簽名以接受status作為參數。

function LeftToRightMoveItems(status) {
...
}

提供狀態后,代碼也存在一些問題。 主要是當varFromBox實際上代表一個選項數組時,才使用varFromBox作為選擇。

例如,最初將varFromBox聲明為對象數組:

var varFromBox = document.getElementById("<%=ListBox1.ClientID%>").options;

然后代碼在不同的地方嘗試訪問options屬性,就像varFromBox是select元素一樣。 實際上,它實際上是在尋找options.options

 while (varFromBox.options.selectedIndex >= 0) {..}

這是我想出的解決這些問題的方法。 我刪除了try / catch,因此任何錯誤都更加明顯。 還要檢查以下示例: http : //jsfiddle.net/7dVyq/

function LeftToRightMoveItems(status){

                if (status == "AddSetup") {
                    var varFromBox = document.getElementById("ListBox1").options;
                    var varToBox = document.getElementById("ListBox2");
                }

                alert(varFromBox.length);
                alert(varToBox.length);

                if ((varFromBox != null)) {
                    if (varFromBox.length < 1) {
                        alert('There are no items to move!');
                        return false;
                    }
                    console.log(varFromBox.selectedIndex);
                    if (varFromBox.selectedIndex == -1) // when no Item is selected the index will be -1
                    {
                        alert('Please select an item to move!');
                        return false;
                    }
                    for (var i = 0; i < varFromBox.length; i++) {
                        if (varFromBox[i].selectedIndex != -1) {
                            var newOption = new Option(); // Create a new instance of ListItem
                            newOption.text = varFromBox[i].text;
                            newOption.value = varFromBox[i].value;
                            varToBox.options[varToBox.length] = newOption; //Append the item in Target Listbox
                            document.getElementById("ListBox1").remove(varFromBox[i]); //Remove the item from Source Listbox
                        }
                    }
                    return false;
                }
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM