2010-05-05 111 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

相关问题