写的一个双向选择器(JS)

本文介绍了一个简单的Web双向选择器实现方案,通过JavaScript控制两个下拉菜单之间的选项移动,支持单选或多选操作,并且可以通过双击来快速移动选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

None.gif<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
<html xmlns="http://www.w3.org/1999/xhtml">
None.gif
<head>
None.gif    
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
None.gif    
<title>Web双向选择器</title>
None.gif
ExpandedBlockStart.gifContractedBlock.gif
<script type="text/javascript" language="javascript">dot.gif
InBlock.gif   
function moveItem(dest, source)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif        
var dest   = ( typeof dest == "string" ? document.getElementById(dest): dest );
InBlock.gif        
var source = ( typeof source  == "string" ? document.getElementById(source) : source );
InBlock.gif
InBlock.gif        
if( source.tagName.toLowerCase() != "select" || dest.tagName.toLowerCase() != "select" )
InBlock.gif            
return;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for ( index=source.length - 1; index >= 0; index-- ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if ( source[index].selected ) dot.gif{
InBlock.gif                dest.length
++;                    
InBlock.gif                dest[dest.length
-1].id    = source[index].id;
InBlock.gif                dest[dest.length
-1].value = source[index].value;
InBlock.gif                dest[dest.length
-1].text  = source[index].text;
InBlock.gif                source[index] 
= null;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
</script>
None.gif
ExpandedBlockStart.gifContractedBlock.gif
<style type="text/css">dot.gif
InBlock.gif
InBlock.gifselect
ExpandedSubBlockStart.gifContractedSubBlock.gif
{dot.gif}{
InBlock.gif    margin
:-2px;
ExpandedBlockEnd.gif
}

None.gif
</style>
None.gif
</head>
None.gif
<body>
None.gif    
<table width="200" border="0" cellpadding="0" cellspacing="0">
None.gif        
<tr>
None.gif            
<td>
None.gif        
None.gif                
<select size="6" multiple="multiple" id="left" ondblclick="moveItem(right,this)">
None.gif                    
<option value="def">ListItem1</option>
None.gif                    
<option value="abcd">ListItem2</option>
None.gif                
</select>
None.gif
None.gif            
</td>
None.gif            
<td>
None.gif
None.gif                
<input type="button" value=" > " name="btnRight" onclick="moveItem(right,left)" />
None.gif               
None.gif                
<input type="button" value=" < " onclick="moveItem(left,right)" id="btn" name="btnLeft" />
None.gif
None.gif            
</td>
None.gif            
<td>
None.gif        
None.gif                
<select size="6" multiple="multiple" id="right" ondblclick="moveItem(left,this)">
None.gif                    
<option value="abc">ListItemm3</option>
None.gif                    
<option value="def">ListItem4</option>
None.gif                    
<option value="abcd">ListItem5</option>
None.gif                    
<option value="abc">ListItem6</option>
None.gif                    
<option value="def">ListItem7</option>
None.gif                    
<option value="abcd">ListItem8</option>
None.gif                
</select>
None.gif        
None.gif            
</td>
None.gif        
</tr>
None.gif    
</table>
None.gif
</body>
None.gif
</html>
None.gif

转载于:https://www.cnblogs.com/CSharp/archive/2006/06/04/416991.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值