javascript操作多选列表框Word下载.docx
《javascript操作多选列表框Word下载.docx》由会员分享,可在线阅读,更多相关《javascript操作多选列表框Word下载.docx(11页珍藏版)》请在冰豆网上搜索。
/**
*使选中的项目上移
*
*oSelect:
源列表框
*isToTop:
是否移至选择项到顶端,其它依次下移,
*true为移动到顶端,false反之,默认为false
*/
functionmoveUp(oSelect,isToTop)
{
//默认状态不是移动到顶端
if(isToTop==null)
varisToTop=false;
//如果是多选------------------------------------------------------------------
if(oSelect.multiple)
for(varselIndex=0;
selIndex&
oSelect.options.length;
selIndex++)
//如果设置了移动到顶端标志
if(isToTop)
if(oSelect.options[selIndex].selected)
vartransferIndex=selIndex;
while(transferIndex&
0&
amp;
!
oSelect.options[transferIndex-1].selected)
oSelect.options[transferIndex].swapNode(oSelect.options[transferIndex-1]);
transferIndex--;
}
//没有设置移动到顶端标志
else
if(selIndex&
0)
if(!
oSelect.options[selIndex-1].selected)
oSelect.options[selIndex].swapNode(oSelect.options[selIndex-1]);
//如果是单选--------------------------------------------------------------------
varselIndex=oSelect.selectedIndex;
=0)
return;
while(selIndex&
selIndex--;
else
/**
*使选中的项目下移
是否移至选择项到底端,其它依次上移,
*true为移动到底端,false反之,默认为false
functionmoveDown(oSelect,isToBottom)
if(isToBottom==null)
varisToBottom=false;
varselLength=oSelect.options.length-1;
for(varselIndex=oSelect.options.length-1;
=0;
selIndex--)
if(isToBottom)
selLength&
oSelect.options[transferIndex+1].selected)
oSelect.options[transferIndex].swapNode(oSelect.options[transferIndex+1]);
transferIndex++;
selLength)
oSelect.options[selIndex+1].selected)
oSelect.options[selIndex].swapNode(oSelect.options[selIndex+1]);
=selLength-1)
selLength-1)
selIndex++;
*移动select的部分内容,必须存在value,此函数以value为标准进行移动
*oSourceSel:
源列表框对象
*oTargetSel:
目的列表框对象
functionmoveSelected(oSourceSel,oTargetSel)
//建立存储value和text的缓存数组
vararrSelValue=newArray();
vararrSelText=newArray();
//此数组存贮选中的options,以value来对应
vararrValueTextRelation=newArray();
varindex=0;
//用来辅助建立缓存数组
//存储源列表框中所有的数据到缓存中,并建立value和选中option的对应关系
for(vari=0;
i&
oSourceSel.options.length;
i++)
if(oSourceSel.options[i].selected)
//存储
arrSelValue[index]=oSourceSel.options[i].value;
arrSelText[index]=oSourceSel.options[i].text;
//建立value和选中option的对应关系
arrValueTextRelation[arrSelValue[index]]=oSourceSel.options[i];
index++;
//增加缓存的数据到目的列表框中,并删除源列表框中的对应项
arrSelText.length;
i++)
//增加
varoOption=document.createElement("
option"
);
oOption.text=arrSelText[i];
oOption.value=arrSelValue[i];
oTargetSel.add(oOption);
//删除源列表框中的对应项
oSourceSel.removeChild(arrValueTextRelation[arrSelValue[i]]);
*移动select的整块内容
functionmoveAll(oSourceSel,oTargetSel)
//存储所有源列表框数据到缓存数组
arrSelValue[i]=oSourceSel.options[i].value;
arrSelText[i]=oSourceSel.options[i].text;
//将缓存数组的数据增加到目的select中
//清空源列表框数据,完成移动
oSourceSel.innerHTML="
"
;
*删除选定项目
functiondeleteSelectItem(oSelect)
if(i&
=0&
=oSelect.options.length-1&
oSelect.options[i].selected)
oSelect.options[i]=null;
i--;
//js文件完毕
/SCRIPT&
metahttp-equiv="
Content-Type"
content="
text/html;
charset=gb2312"
BODYstyle="
font-size:
12px"
FORMname="
form1"
method="
post"
action="
SELECTname="
left"
size="
10"
id="
select"
multiplestyle="
width:
100px;
"
OPTIONvalue="
aaaaa"
aaaaa&
/OPTION&
bbbbb"
bbbbb&
ccccc"
ccccc&
/SELECT&
INPUTstyle="
border:
1pxsolidblack"
type="
button"
value="
onClick="
moveSelected(document.all.left,document.all.right)"
moveSelected(document.all.right,document.all.left)"
right"
ddddd"
ddddd&
eeeee"
eeeee&
fffff"
fffff&
ggggg"
ggggg&
hhhhh"
hhhhh&
iiiii"
iiiii&
br&
DIVstyle="
background-color:
#CCCCCC;
padding:
2px"
上移一格"
moveUp(document.all.right);
moveUp(document.all.left)"
下移一格"
moveDown(document.all.right);
moveDown(document.all.left)"
上移到顶"
moveUp(document.all.right,true);
moveUp(document.all.left,true)"
下移到顶"
moveDown(document.all.right,true);
moveDown(document.all.left,true);
(支持多选移动)
/DIV&
BR&
padding:
5px;
width:
100%;
position:
relative"
右移:
INPUTtype="
radio"
name="
ifAll"
checked&
左移:
移动全部"
style="
judgeMove()"
5px"
删除"
deleteSelectItem(document.all.left);
deleteSelectItem(document.all.right)"
/div&
/FORM&
/BODY&
functionjudgeMove()
vararrRadio=document.all.ifAll;
varvalOfRadio;
arrRadio.length;
if(arrRadio[i].checked)
valOfRadio=arrRadio[i].value;
break;
if(valOfRadio=="
)
moveAll(document.all.right,document.all.left);
moveAll(document.all.left,document.all.right);