WEB开发网
开发学院网页设计JavaScript 用Javascript实现ListBox的增或者是删 阅读

用Javascript实现ListBox的增或者是删

 2010-09-14 13:26:08 来源:WEB开发网   
核心提示:假设有2个ListBox,ID分别为lbLeft,用Javascript实现ListBox的增或者是删,lbRight 选中lbLeft里的项添加到lbRight里:function add(){ var left; var right; left = document.getElementById("lbL

假设有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

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