用 Javascript 实现的“Dual listbox”(双向选择器)

来源:互联网 发布:刷计步器步数有软件么 编辑:程序博客网 时间:2024/05/16 12:22

    这是我用 Javascript 制作的“Dual listbox”(双向选择器)的一个应用示例,是从我的代码中抠出来的。在网页编程中经常会用到。

    也许我的实现太烦琐了,希望大家有更好的代码贡献出来。

<html>
<head>
  <title>选择器</title>
  <link href="./style/style.css" rel="stylesheet" type="text/css">
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  <meta http-equiv="Page-Enter" content="blendTrans(Duration=1.0)">
  <meta http-equiv="Page-Exit" content="blendTrans(Duration=1.0)">
  <script language="javascript">
    function openwin(url, l, t, w ,h)
    {open(url,'','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width='+ w +',height='+ h +',left='+ l +',top='+ t);}

    function check_and_submit(frm)
    {
      SelectAll(frm.SelectedItem);
      frm.submit();
    }
  </script>
</head>

<body>

<form name="frm1" id="frm1" method="post" action="save.asp">
<input name="allowsubmit" type="hidden" value="OK">

<table width="500" border="0" cellspacing="0" cellpadding="0" align="center">
  <tr height=10><td colspan=3></td></tr>
  <tr>
    <td width="220" align=center valign="top">
      已分配该用户管理的栏目:<br><br>
      <select name="SelectedItem" id="SelectedItem" size=12 multiple="true">
        <option>无</option>

      </select>
      </select>
    </td>
    <td width="60" align=center>
      <br><br>
      <button onClick="MoveSingleItem(WaitSelectItem, SelectedItem)">&lt;</button><br><br>
      <button onClick="MoveAllItems(WaitSelectItem, SelectedItem)">&lt;&lt;</button><br><br><br><br>
      <button onClick="MoveSingleItem(SelectedItem, WaitSelectItem)">&gt;</button><br><br>
      <button onClick="MoveAllItems(SelectedItem, WaitSelectItem)">&gt;&gt;</button><br>
    </td>
    <td width="220" align=center valign="top">
      待分配的栏目:<br><br>
      <select name="WaitSelectItem" size=12 multiple=true>
        <option>师大要闻</option>
        <option>视频新闻</option>
        <option>图片新闻</option>
        <option>专题要闻</option>
        <option>十万个为什么</option>
        <option>代码测试</option>
        <option>www.why100000.com</option>
      </select>
    </td>
  </tr>
</table>
</form>

<script language="javascript">
 function MoveSingleItem(sel_source, sel_dest)
 {
   if (sel_source.selectedIndex==-1)  //源:没有点选任何项目
     return;

   if (sel_source.options[0].text=="无") //源:只有“无”项目
     return;

   if (sel_dest.options[0].text=="无") //目标:只有“无”项目,则先删除该提示性项目
     sel_dest.options.remove(0);

   var SelectedText = sel_source.options[sel_source.selectedIndex].text;
   sel_dest.options.add(new Option(SelectedText));
   sel_source.options.remove(sel_source.selectedIndex);

   if (sel_source.options.length==0)  //源:如果删除完所有有用项目,则添加提示项目:“无”
     sel_source.options.add(new Option("无"));
 }

 function MoveAllItems(sel_source, sel_dest)
 {
   if (sel_source.options[0].text=="无") //源:只有“无”项目
     return;

   if (sel_dest.options[0].text=="无")   //目标:只有“无”项目,则先删除该提示性项目
     sel_dest.options.remove(0);

   //首先拷贝所有项目到目标:
   var sel_source_len = sel_source.length;
   for (var j=0; j<sel_source_len; j++)
   {
     var SelectedText = sel_source.options[j].text;
     sel_dest.options.add(new Option(SelectedText));
   }

   //然后删除“源”所有项目:
   while ((k=sel_source.length-1)>=0)
   {
     if (sel_source.options[0].text=="无") //源:只有“无”项目
       break;
     sel_source.options.remove(k);
     if (sel_source.options.length==0)  //源:如果删除完所有有用项目,则添加提示项目:“无”
       sel_source.options.add(new Option("无"));
   }
 }

 function SelectAll(theSel)  //选中select中全部项目
 { for (i = 0 ;i<theSel.length;i++)
    theSel.options[i].selected = true;
 }
</script>


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机显示无法解析dns地址怎么办 台式电脑宽带连接不上怎么办 电信宽带账号登录密码忘记了怎么办 电信校园宽带超时了怎么办 宽带连接账号密码忘了怎么办 移动宽带路由器上不了网怎么办 移动宽带太卡了怎么办 电信adsl密码忘记了怎么办 移动宽带无法连接网络怎么办 移动宽带电视无信号怎么办 联通网线故障电话打不通怎么办 w10系统ip地址错误怎么办 移动流量太贵了怎么办 修改wifi密码ip地址怎么办 苹果6s接电话声音小怎么办 k歌录音器失败怎么办 想报警但不能说话怎么办 微粒贷要家人电话怎么办 4g网络信号差怎么办 手机移动网络信号不好怎么办 移动的4g网络差怎么办 4g移动网络慢怎么办 房间没有4g网络怎么办 oppo显示2g网络怎么办 oppo只有2g网络怎么办 移动卡4g网络慢怎么办 易信专线电话用完了怎么办 手机拨打电话时黑屏怎么办 网易号文章一直审核中怎么办 登陆积分会员忘记密码怎么办 易信密码忘记了怎么办 滴滴永久封停的号怎么办 微信版本过低怎么办 报关时通关单号没录怎么办 公司注销了行政许可证怎么办呢 报关项数超过50项怎么办 出口报关件数报多了怎么办 关税少交被海关缉私查到怎么办 外贸报关hs编码报错怎么办 报关金额少报了怎么办 进口报关金额少报了怎么办