`
pcajax
  • 浏览: 2106930 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

成长手删代码40-listbox js操作上下左右移动.txt

阅读更多
<script type ="text/javascript">
    function imgRight()
    {
       var ddlRight=document.getElementById ("LBRight");
       var ddlLeft=document.getElementById ("LBLeft");
       var count = 0;
       for(var i=0;i<ddlLeft.length;i++)
       {
            count = 0;
            if(ddlLeft[i].selected)
            {
                //查ddlRight在右边是否存在要加的项,如果存在,提示,不加
               
                if(ddlRight.length==0)
                {
                    ddlRight.options.add(new Option(ddlLeft[i].text,ddlLeft[i].value));
                    document.getElementById ("hiddenLBRightValue").value+=ddlLeft[i].value+","
                }
                else
                {
                     for(var intRight=0;intRight<ddlRight.length;intRight++)
                    {
                        if(ddlRight[intRight].text==ddlLeft[i].text)
                        {
                          count++;
                        }
                      
                    }
                    if(count==0)
                    {
                        ddlRight.options.add(new Option(ddlLeft[i].text,ddlLeft[i].value));
                        document.getElementById ("hiddenLBRightValue").value+=ddlLeft[i].value+","
                    }
                }
            }
       }
       
    }
   
   
    function imgLeft()
    {
        var ddlRight=document.getElementById ("LBRight");
       
        for(var i=0;i<ddlRight.length;i++)
       {
            if(ddlRight[i].selected)
            {
               
                ddlRight.remove(ddlRight.selectedIndex);
                i--;
            }
       }
       
    }
   
   
    function imgUp()
    {
        var ddlRight=document.getElementById ("LBRight");
        var count=0;
       
        if(ddlRight.length!=0)
        {
            for(var i=0;i<ddlRight.length;i++)
            {
                if(ddlRight.selectedIndex!=0)
                {
                    if(ddlRight[i].selected)
                    {
                        count++;
                    }
                }
                else
                {
                    return ;
                }
               
            }
           
            if(count!=0)//选中的不是第一项
            {
                if(ddlRight.selectedIndex<=ddlRight.length-1)
                {
                    //ddlRight.options(ddlRight.selectedIndex).swapNode(ddlRight.options(ddlRight.selectedIndex-1));
                    var tempSelect=document.createElement ("option");
                    tempSelect.text=ddlRight.options[ddlRight.selectedIndex].text;
                    tempSelect.value=ddlRight.options[ddlRight.selectedIndex].value;
                   
                    ddlRight.options[ddlRight.selectedIndex].text=ddlRight.options[ddlRight.selectedIndex-1].text;
                    ddlRight.options[ddlRight.selectedIndex].value=ddlRight.options[ddlRight.selectedIndex-1].value;
                   
                    ddlRight.options[ddlRight.selectedIndex-1].text=tempSelect.text;
                    ddlRight.options[ddlRight.selectedIndex-1].value=tempSelect.value;
                   
                    ddlRight.selectedIndex=ddlRight.selectedIndex-1;
                   
                }
            }
            else
            {
                alert("请先选择要上移的项");
            }
        }
        else
        {
            alert("该列表中没有数据");
        }
    }
    function imgDown()
    {
        var ddlRight=document.getElementById ("LBRight");
        var count=0;
       
        if(ddlRight.length!=0)
        {
            for(var i=0;i<ddlRight.length;i++)
            {
                if(ddlRight.selectedIndex!=ddlRight.length-1)
                {
                    if(ddlRight[i].selected)
                    {
                        count++;
                    }
                }
                else
                {
                    return ;
                }
               
            }
           
            if(count!=0)//选中的不是第一项
            {
                if(ddlRight.selectedIndex<=ddlRight.length-1)
                {
                    //ddlRight.options(ddlRight.selectedIndex).swapNode(ddlRight.options(ddlRight.selectedIndex-1));
                    var tempSelect=document.createElement ("option");
                    tempSelect.text=ddlRight.options[ddlRight.selectedIndex].text;
                    tempSelect.value=ddlRight.options[ddlRight.selectedIndex].value;
                   
                    ddlRight.options[ddlRight.selectedIndex].text=ddlRight.options[ddlRight.selectedIndex+1].text;
                    ddlRight.options[ddlRight.selectedIndex].value=ddlRight.options[ddlRight.selectedIndex+1].value;
                   
                    ddlRight.options[ddlRight.selectedIndex+1].text=tempSelect.text;
                    ddlRight.options[ddlRight.selectedIndex+1].value=tempSelect.value;
                   
                    ddlRight.selectedIndex=ddlRight.selectedIndex+1;
                   
                }
            }
            else
            {
                alert("请先选择要上移的项");
            }
        }
        else
        {
            alert("该列表中没有数据");
        }
       
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics