前一段时间整理了一个js,主要是用来操作两个select表单的option节点相互移动,及置顶置地 向上向下移动
1.js代码
/**
* @功能 select左右移动实现js
* @作者 md_java
* @时间 2010-6-20
*/
var fromsob,toobj,froms,arry,nowIndex,newoption;
/**
* @功能 分批添加或删除
* @输入 formselect 要移动的select;toselect 要移给的select,postionstr 移动指向left OR right
* @返回 无
*/
function moveoption(formselect,toselect,postionstr){
fromsobj=document.getElementById(formselect);
toobj=document.getElementById(toselect);
if(postionstr=='left'&&fromsobj.options.length==0){//判断是否还有权限
alert('left is empty');
return;
}else if(postionstr=='right'&&fromsobj.options.length==0){//判断是否还有权限
alert('right is empty');
return;
}
if(postionstr=='left'&&fromsobj.options.selectedIndex==-1){//判断是否选中
alert('please choose left');
return;
}else if(postionstr=='right'&&fromsobj.options.selectedIndex==-1){//判断是否选中
alert('please choose right');
return;
}
froms=fromsobj.options;//获得要移动select的opations
arry=new Array();
var arr=0;
for(var i=0;i<froms.length;i++){
if(froms[i].selected==true){
nowIndex = toobj.options.length; //获取将要移动到toselect的options长度
newoption = new Option(froms[i].text, froms[i].value,false,false);//copy option
toobj.options[nowIndex] = newoption;
fromsobj
arry[arr]=froms[i];//将选中对象缓存如数组 等下删除
arr++;
}
}
//删除已被移动的option
for(var a=0;a<arry.length;a++){
fromsobj.options[arry[a].index]=null;
}
}
/**
* @功能 整体移动option全部添加或全部删除
* @输入 formselect 要移动的select;toselect 要移给的select,postionstr 移动指向left OR right
* @返回 无
*/
function giveOrUndoAll(formselect,toselect,postionstr){
fromsobj=document.getElementById(formselect);
toobj=document.getElementById(toselect);
if(postionstr=='left'&&fromsobj.options.length==0){
alert('left is empty');
return;
}
if(postionstr=='right'&&fromsobj.options.length==0){
alert('right is empty');
return;
}
froms=fromsobj.options;
for(var i=0;i<froms.length;i++){
nowIndex = toobj.options.length;
newoption = new Option(froms[i].text, froms[i].value,false,false);
toobj.options[nowIndex] = newoption;
}
fromsobj.innerHTML=null;
}
/**单元素向底部移动*/
function mTop(sid){
selectRight=document.getElementById(sid);
var i = selectRight.options.selectedIndex;
if(i > 0){
Temp_Text=selectRight.options(i).text;
Temp_ID=selectRight.options(i).value;
for(j=i;j>0;j--){
selectRight.options(j).text=selectRight.options(j-1).text;
selectRight.options(j).value=selectRight.options(j-1).value;
}
selectRight.options(0).value=Temp_ID;
selectRight.options(0).text=Temp_Text;
selectRight.selectedIndex=0;
}
}
/**单元素向上移动*/
function mUp(sid){
selectRight=document.getElementById(sid);
var i = selectRight.options.selectedIndex;
var j = i-1
if(i>0){
Temp_Text = selectRight.options(j).text;
Temp_ID = selectRight.options(j).value;
selectRight.options(j).text = selectRight.options(i).text;
selectRight.options(j).value = selectRight.options(i).value;
selectRight.options(i).text = Temp_Text;
selectRight.options(i).value = Temp_ID;
selectRight.selectedIndex=j;
}
}
/**单元素向下移动*/
function mDown(sid){
selectRight=document.getElementById(sid);
var i = selectRight.options.selectedIndex;
if (i != selectRight.length-1){
var j = i+1;
if(i < selectRight.length){
Temp_Text = selectRight.options(j).text;
Temp_ID = selectRight.options(j).value;
selectRight.options(j).text = selectRight.options(i).text;
selectRight.options(j).value = selectRight.options(i).value;
selectRight.options(i).text = Temp_Text;
selectRight.options(i).value = Temp_ID;
selectRight.selectedIndex=j;
}
}
}
/**单元素向底部移动*/
function mBottom(sid){
selectRight=document.getElementById(sid);
var i = selectRight.selectedIndex;
var j = selectRight.length-1
if(i < j){
Temp_Text = selectRight.options(i).text;
Temp_ID = selectRight.options(i).value;
for(var k=i+1;k<=j;k++){
selectRight.options(k-1).text=selectRight.options(k).text;
selectRight.options(k-1).value=selectRight.options(k).value;
}
selectRight.options(j).text=Temp_Text;
selectRight.options(j).value=Temp_ID;
selectRight.selectedIndex=j;
}
}
2.html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MOVE SELECT</title>
<script type="text/javascript" src="opationSelect.js"></script>
<style type="text/css">
<!--
.STYLE3 {
font-size: 14px;
font-weight: bold;
}
li{text-align:center}
input{text-align:center;width:80px}
-->
</style>
<script type="text/javascript">
function checkSub(){
var Vs=document.getElementById("rselect").options;
var str="you want move:\n";
for(var i=0;i<Vs.length;i++){
Vs[i].selected=true;
str=str+"\n"+Vs[i].text;
}
alert(str);
return false;
}
</script>
</head>
<body>
<form action="" method="post" id="form1">
<table width="100%" border="1" align="center" cellpadding="0"
cellspacing="0">
<tr>
<td height="18" colspan="2" align="center"
style="padding-bottom: 20px">
<span class="STYLE3">MOVE SELECT</span>
</td>
</tr>
<tr>
<td height="35" >
<table border="1" cellpadding="0" align="center" cellspacing="0" >
<tr align="center">
<th>
left
</th>
<th>
MOVE
</th>
<th>
right
</th>
</tr>
<tr>
<td>
<select multiple="multiple"
style="height: 200px; width: 120px" id="lselect">
<option value="1">
opation1
</option>
<option value="2">
opation2
</option>
<option value="3">
opation3
</option>
<option value="4">
opation4
</option>
<option value="5">
opation5
</option>
<option value="6">
opation6
</option>
</select>
</td>
<td align="left">
<ul style="list-style:none;margin:0px; padding:0px;">
<li><input type="button" value="toTop" onclick="mTop('rselect');" /></li>
<li><input type="button" value="toUp" onclick="mUp('rselect');" /></li>
<li><input type="button" value="toLeftAll" onclick="giveOrUndoAll('lselect','rselect','left');" />
</li>
<li><input type="button" value="toLeft" onclick="moveoption('lselect','rselect','left');" />
</li>
<li><input type="button" value="toRight" onclick="moveoption('rselect','lselect','right');" /></li>
<li><input type="button" value="toRightAll" onclick="giveOrUndoAll('rselect','lselect','right');" /></li>
<li><input type="button" value="toDown" onclick="mDown('rselect');" /></li>
<li><input type="button" value="toButtom" onclick="mBottom('rselect');" /></li>
</ul>
</td>
<td>
<select multiple="multiple"
style="height: 200px; width: 120px" id="rselect">
</select>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="35" align="center">
<input value="submit" type="button" onclick="checkSub();" />
</td>
</tr>
</table>
</form>
</body>
</html>
分享到:
相关推荐
js控制select选中显示不同表单内容select下拉菜单特效 js控制select选中显示不同表单内容select下拉菜单特效 js控制select选中显示不同表单内容select下拉菜单特效
js控制select相关方法,如,控制选中,动态增加,动态减少,删除等等。web开发利器啊
对select框内元素上下移动排序
select选择框内容左右移动源码,请大家参考。
类似word选项的左移右移功能 经典list 到 list 的移动,并且支持上移和下移。 大家学习学习。
Js操作Select大全(取值、设置选中等等) Js操作Select大全(取值、设置选中等等)
select选择框内容左右移动添加删除代码基于jquery-1.8.3.min.js实现,简单实用,选中选项内容,点击移动按钮可进行内容左右移动,双击option内容也可左右移动,支持单选移动、多选移动和一键全部移动!
本文实例讲述了JS实现Select的option上下移动的方法。分享给大家供大家参考,具体如下: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> [removed] function UpOr...
js操作select控件大全 js操作select控件大全 js操作select控件大全 js操作select控件大全 js操作select控件大全 js操作select控件大全
Select上移下移 jQuerySelect jQuerySelect移动 Select上下移动
JS模拟select下拉菜单
JS 重写 SELECT 方便自定义SELECT样式
js 操作select 下拉框js 操作select 下拉框
涵盖了js对select的常用操作 1.判断select选项中 是否存在Value="paraValue"的Item 2.向select选项中 加入一个Item 3.从select选项中 删除一个Item 4.修改select选项中 value="paraValue"的text为"paraText" 5.设置...
Js实现两个Select标签中的选项之间的移动
功能类似struts2 optiontransferselect 标签的jquery组件 非常好用的一个JQuery组件
jquery.selectbox.js select下拉菜单美化代码 jquery.selectbox.js select下拉菜单美化代码 jquery.selectbox.js select下拉菜单美化代码