当前位置导航:炫浪网>>网络学院>>网页制作>>JavaScript教程

javascript对ListBox的增 删

javascript 对ListBox的增 删

假设有2个ListBox,ID分别为lbLeft,lbRight

选中lbLeft里的项添加到lbRight里:

 function add()

    {

        var left;

        var right;

        left = document.getElementById("lbLeft");

        right = document.getElementById("lbRight");

        for(var i=0;i<left.length;i++)

        {

            if(left.options[i].selected)

            {

                //判断该项是否已经存在

                for(j=0;j<right.length;j++)

                {

                    if(right.options[j].value==left.options[i].value)

                    {

                        return;

                    }

                }

                right.add(new Option(left.options[i].text,left.options[i].value));

            }

        }

    }

 

删除lbRight里的项:

 

    function del()

    {

        right=document.getElementById("lbRight");

        for(i=right.length-1;i>=0;i--)

        {

            if(right.options[i].selected)

            {

                right.remove(i);

            }

        }

        //重新赋值

        for(j=0;j<right.length;j++)

        {

            document.getElementById("txtAllName").value+=right.options[j].text+",";

            document.getElementById("txtAllID").value+=right.options[j].value+",";

        }

    }

相关内容
赞助商链接