javascript 对ListBox的增 删
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/webkaifa/)假设有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;ileft.length;i++)
{
if(left.options[i].selected)
{
//判断该项是否已经存在
for(j=0;jright.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);
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/webkaifa/)}
}
//重新赋值
for(j=0;jright.length;j++)
{
document.getElementById("txtAllName").value+=right.options[j].text+",";
document.getElementById("txtAllID").value+=right.options[j].value+",";
}
}