高分请教!Select框复制问题
有2个select框SourceSelect和TargetSelect
问题:将SourseObject中的一项或多项复制到TargetObject,SourseObject中的项存在依附关系,如1000,1100,1110,1111。1000为根节点,如何实现当用户选择1111时,如果目标select中不存在1000,1100,1110,则将这些值同时拷贝到目标select中。复制时用到如下函数:
/*
*多选下拉列表动态增加函数:Create(SourceSelect,TargetSelect);
*参数:SourseObject:表示要动态添加的源Select;
*参数:TargetObject:表示要动态接受的目的Select;
*例:Create(document.all.SourSelect,document.all.TarSelect);
*/
function Create(SourceSelect,TargetSelect)
{
var IsCreate = true;
var theText = "";
var theIndex = SourceSelect.selectedIndex;
var theLength = SourceSelect.length ;
if (theIndex == -1 ) //如果源Select为空的话,则退出过程
return false;
while (IsCreate) //添加到目的Select循环
{
theText = SourceSelect.options[theIndex].text;
theValue = SourceSelect.options[theIndex].value;
for (i = 0 ;i < TargetSelect.length ; i++)
{
HaveIt = true;
if (TargetSelect.options[i].text == theText)
{
alert(theText+":目标select中已存在此项!");
HaveIt = false;
break;
}
else
{
HaveIt = true;
}
}
if (HaveIt)//如果目标select不存在该项,则增加
TargetSelect.options.add(new Option(theText,theValue));
document.all.func_added.options.add(new Option(theText,theValue));
theIndex = theIndex + 1; //如果是选择多列的话,对下一个进行处理
if (theIndex == theLength)
{ //如果两者想等的话,则源Select多了一个值,
IsCreate = false; //所以需要退出循环
break;
}
if (SourceSelect.options[theIndex].selected == false)//如果没有被选择的话,则退出循环
{
IsCreate = false;
}
}
}