2010-05-05 120 views
0

我有兩個列表框(A,B)與一些值,我可以發送值從A到B或B到A ,我有一個保存按鈕。我有兩個列表框(A,B)與一些值,我可以發送值從A到B或B到A

第一次不更新任何列表框,如果我點擊保存按鈕我顯示的消息,如「沒有變化或完成」

,但一旦我從A發送一個項目B和重新發送相同的項目從B到A這意味着沒有變化或完成。這裏我也想顯示同樣的消息「沒有改變或完成」。但我無法罰款staus可以任何一個pleasse給代碼或提示找到列表框在JavaScript中的默認狀態。

感謝

回答

0

我不知道爲什麼你想與客戶端JavaScript來做到這一點,但這裏有一個例子: lbClicked是JS-功能,該功能的onclick呼籲列表框項刪除的項目和將它們添加到其他列表框。最重要的是我已經在服務器端註冊了兩個JavaScript數組(Lb1Items和Lb2Items),它們擁有列表框項目的值。當用戶點擊提交按鈕(或曾經)我比較每個列表框與相應的數組。當一切都一樣時,什麼都沒有改變。

function lbClicked(sender){ 
     var lb1=document.getElementById("ListBox1"); 
     var lb2=document.getElementById("ListBox2"); 
     if(lb1 != null && lb2 != null){ 
      if(sender==lb1){ 
       var selectedItem=lb1.item(lb1.selectedIndex); 
       if(! contains(Lb2Items,selectedItem.value)){ 
        lb1.remove(lb1.selectedIndex); 
        lb2.add(selectedItem); 
       } 
      }else if(sender==lb2){ 
       var selectedItem=lb2.item(lb2.selectedIndex); 
       if(! contains(Lb1Items,selectedItem.value)){ 
        lb2.remove(lb2.selectedIndex); 
        lb1.add(selectedItem); 
       } 
      } 
     } 
    } 


    function Button1_onclick() { 
     var changed=itemsChanged(); 
     if(changed) 
      alert("Items Changed!"); 
     else 
      alert("Items not changed!"); 
     return changed; 
    } 

    function itemsChanged(){ 
     if(Lb1Items != null && Lb2Items != null){ 
      var lb1=document.getElementById("ListBox1"); 
      var lb2=document.getElementById("ListBox2"); 
      if(lb1 != null && lb2 != null){ 
       if(lb1.options.length != Lb1Items.length || lb2.options.length != Lb2Items.length) 
        return true; 

       for(var i=0; i<lb1.options.length; i++){ 
        var item=lb1.options[i]; 
        if(! contains(Lb1Items,item.value)) 
         return true; 
       } 
       for(var i=0; i<lb2.options.length; i++){ 
        var item=lb2.options[i]; 
        if(! contains(Lb2Items,item.value)) 
         return true; 
       } 
      } 
     } 
     return false; 
    } 

    function contains(objArray,objectToFind){ 
     for(var i=0;i<objArray.length;i++){ 
       if(objArray[i]==objectToFind) 
        return true; 
     } 
     return false; 
    } 

測試與ASP.Net:

<form id="form1" runat="server"> 
     <asp:ListBox ID="ListBox1" runat="server" onclick="javascript:lbClicked(this);" Height="84px"> 
      <asp:ListItem Text="Item 1" Value="1"></asp:ListItem> 
      <asp:ListItem Text="Item 2" Value="2"></asp:ListItem> 
      <asp:ListItem Text="Item 3" Value="3"></asp:ListItem> 
      <asp:ListItem Text="Item 4" Value="4"></asp:ListItem> 
      <asp:ListItem Text="Item 5" Value="5"></asp:ListItem> 
     </asp:ListBox>&nbsp; 
     <asp:ListBox ID="ListBox2" runat="server" onclick="javascript:lbClicked(this);" Height="82px"> 
      <asp:ListItem Text="Item 6" Value="6"></asp:ListItem> 
      <asp:ListItem Text="Item 7" Value="7"></asp:ListItem> 
      <asp:ListItem Text="Item 8" Value="8"></asp:ListItem> 
      <asp:ListItem Text="Item 9" Value="9"></asp:ListItem> 
      <asp:ListItem Text="Item 10" Value="10"></asp:ListItem> 
     </asp:ListBox> 
     &nbsp; 
     <input id="Button1" type="button" value="button" onclick="return Button1_onclick()" /> 
    </form> 

和陣列登記在服務器端是這樣的:

ClientScript.RegisterArrayDeclaration("Lb1Items", "1,2,3,4,5") 
ClientScript.RegisterArrayDeclaration("Lb2Items", "6,7,8,9,10") 

問候, 添

編輯:在itemsChanged-function和che中添加了相同數量的項目ck如果目標列表框已經包含要傳輸的項目

+0

是的,我們有權利,但如果我們在這兩個列表框中有重複,那麼它將如何工作。 – Dev 2010-05-07 07:11:49

+0

爲什麼你有重複的值?通常,當所選項目已經在目標列表框中時,您不需要將項目從一個列表框轉移到另一個列表框。如果項目已經在目標列表框中(相同的值),我已經在每個列表框中添加了相同數量的項目的檢查,並在lbClicked中添加了一個檢查。 – 2010-05-07 07:35:05

相關問題