js操作ListBox 2


    //经营分类添加
    function AddItem()
    {
        var lsitChose=document.getElementById("lsitChose");
        var lsitChoseed=document.getElementById("lsitChoseed");
        var hide=document.getElementById("hide");
        var count=lsitChoseed.options.length;
        var flag=0;
        var value;
        var text;
        if(lsitChose.options.length==0)
        {
            alert("请选择商品分类");
        }
        else
        {
            for(var i=0;i<lsitChose.options.length;i++)
            {
                if(lsitChose.options[i].selected==true)
                {
                    flag=flag+1;
                }
            }
            if(flag==0)
            {
                alert("请选择要添加的项");
            }
            else if(flag==1)
            {
                value=lsitChose.options[lsitChose.selectedIndex].value;
                text=lsitChose.options[lsitChose.selectedIndex].text;
               if(hide.value.indexOf(value)<0)
               {
                   lsitChoseed.options[count]=new Option(text,value,true,true);
                   hide.value+=value+",";
               }
            }
            else if(flag>1)
            {
                    var up=0;
                    for(var j=0;j<lsitChose.options.length;j++)
                    {
                        if(lsitChose.options[j].selected==true)
                        {
                           up+=1;
                           value=lsitChose.options[j].value;
                           text=lsitChose.options[j].text;
                           if(hide.value.indexOf(value)<0)
                           {
                               lsitChoseed.options[count+up-1]=new Option(text,value,true,true);
                               hide.value+=value+",";
                           }
                           else
                           {
                            up-=1;
                           }
                        }
                    }
                  
            }                  
        }   
     }
   
   
    //经营分类移除
     function DeleteItem()
    {
        var lsitChoseed=document.getElementById("lsitChoseed");
        var hide=document.getElementById("hide");
        var flag=0;
      
       
        for(var i=0;i<lsitChoseed.options.length;i++)
        {
             if(lsitChoseed.options[i].selected==true)
             {
                flag+=1;
                hide.value=hide.value.replace(lsitChoseed.options[i].value+",","");
                lsitChoseed.options[i].parentNode.removeChild(lsitChoseed.options[i]);
               
             }
        }
       
        if(flag==0)
        {
            alert("请选择要移除的项");
        }
       
     }
    
     //添加所有项
     function AddAllItems()
     {
        var lsitChose=document.getElementById("lsitChose");
        var lsitChoseed=document.getElementById("lsitChoseed");
        var count=lsitChoseed.options.length;
        var hide=document.getElementById("hide");
       
        var up=0;
        for(var i=0;i<lsitChose.options.length;i++)
        {
          
            var value=lsitChose.options[i].value;
            var text=lsitChose.options[i].text;
            if(hide.value.indexOf(value)<0)
            {
                 up+=1;
                 lsitChoseed.options[count+up-1]=new Option(text,value,true,true);
                 hide.value+=value+",";
            }
           
        }
     }
    
     //清除所有项
     function CleanItems(list)
     {
        document.getElementById(list).options.length=0;
        document.getElementById("hide").value="";
     }
     
     
    

你可能感兴趣的:(listbox)