WEB开发网
开发学院网页设计JavaScript javascript对ListBox的增 删 阅读

javascript对ListBox的增 删

 2008-08-18 19:57:13 来源:WEB开发网   
核心提示:javascript 对ListBox的增 删 假设有2个ListBox,ID分别为lbLeft,javascript对ListBox的增 删,lbRight选中lbLeft里的项添加到lbRight里: function add() { var left; var right; left = document

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+",";

   }

   }

Tags:javascript ListBox

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接