在for (i=0;i<fromList.options.length;i++)里面加一个循环,交叉判断下就可以看你基础不错 ,应该可以很快写出来的

解决方案 »

  1.   

    顺便给你个函数:
    //检查option是否存在于select
    var CheckOption = function(Item ,Select){
        for(var i=0;i<Select.options.length;i++)
        {
            if(Item.value == Select.options[i].value)
            {
                return false;
            }
        }
        return true;
    };
      

  2.   

    <html>
    <head>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <title>用户角色管理</title>
    </head><script language="javascript">
    function copyToList(from,to)
    {
    fromList = eval('document.forms[0].' + from);
    toList = eval('document.forms[0].' + to);
    for(var i=fromList.length-1;i>-1;i--)
    if(fromList.options[i].selected)
    toList.appendChild(fromList.options[i]).selected = false;
    }window.onload = function()
    {
    //页面加载后运行从右到左的移动方法
    copyToList("dxz","kczz");
    }</script>            
    <form id="userRoleQuery" name="form1" onSubmit="return true;" action="/ether/userRoleQuery.html" method="post"><table class="wwFormTable">
     <table width="400" border="0" cellpadding="0" cellspacing="0"  align="center">
        <tr>
           <td align="center" style="color: black;">待选组</td>
           <td width="92">&nbsp;</td>
           <td align="center" style="color: black;">可操作组</td>
        </tr>
        <tr>
           <td width="225" align="center">
       <!--这里注意一下 在生成的时候把右边框初始化加载的那些选项 selected设置为true就行了-->
            <select name="dxz"  multiple="multiple" size="15" ondblclick="javascript:copyToList('dxz','kczz')" style="width:90%;">
    <option value="1">会员</option>
    <option value="2" selected>员工</option>
    <option value="3">管理员</option>
    <option value="4" selected>超级用户</option>
    <option value="5">超级管理员</option>
    </select>
           </td>
        <td width="92" align="center" valign="middle">
            <input type="button" name="xzgroup" value="选择组>>" onClick="javascript:copyToList('dxz','kczz')" class="btn">
            <br><br><br><br>
            <input type="button" name="qxgroup" value="<<取消组" onClick="javascript:copyToList('kczz','dxz')" class="btn">
        </td>
                                    
        <td width="230" align="center">
        <select name="kczz" multiple="multiple" size="15" ondblclick="javascript:copyToList('kczz','dxz')" style="width:90%;">
        </select></td>
        </tr>
        </table>
    </form>
    </body>
    </html>
      

  3.   

    <html>
    <head>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <title>用户角色管理</title>
    </head><script language="javascript">function copyToList(from,to)
    {
    fromList = eval('document.forms[0].' + from);
    toList = eval('document.forms[0].' + to);
    for(var i=fromList.length-1;i>-1;i--)
    {
    var opt = fromList.options[i];
    if(opt.selected)
    {
    toList.hashtable[opt.value] = opt;
    toList.appendChild(fromList.options[i]).selected = false;
    delete fromList.hashtable[opt.value];
    }
    }
    }window.onload = function()
    {
    var list = document.forms[0].dxz;
    var list1 = document.forms[0].kczz;
    list.hashtable = {};
    list1.hashtable = {};
    for(var i=0,l=list.length;i<l;i++)
    {
    var opt = list.options[i];
    list.hashtable[opt.value] = opt;
    }

    for(var i=0,l=list1.length;i<l;i++)
    {
    var opt = list1.options[i];
    list1.hashtable[opt.value] = opt;
    }

    for(o in list1.hashtable)
    {
    list.removeChild(list.hashtable[o]);
    delete list.hashtable[o];
    }
    }</script>            
    <form id="userRoleQuery" name="form1" onSubmit="return true;" action="/ether/userRoleQuery.html" method="post"><table class="wwFormTable">
     <table width="400" border="0" cellpadding="0" cellspacing="0"  align="center">
        <tr>
           <td align="center" style="color: black;">待选组</td>
           <td width="92">&nbsp;</td>
           <td align="center" style="color: black;">可操作组</td>
        </tr>
        <tr>
           <td width="225" align="center">
       <!--这里注意一下 在生成的时候把右边框初始化加载的那些选项 selected设置为true就行了-->
            <select name="dxz"  multiple="multiple" size="15" ondblclick="javascript:copyToList('dxz','kczz')" style="width:90%;">
    <option value="1">会员</option>
    <option value="2">员工</option>
    <option value="3">管理员</option>
    <option value="4">超级用户</option>
    <option value="5">超级管理员</option>
    </select>
           </td>
        <td width="92" align="center" valign="middle">
            <input type="button" name="xzgroup" value="选择组>>" onClick="javascript:copyToList('dxz','kczz')" class="btn">
            <br><br><br><br>
            <input type="button" name="qxgroup" value="<<取消组" onClick="javascript:copyToList('kczz','dxz')" class="btn">
        </td>
                                    
        <td width="230" align="center">
        <select name="kczz" multiple="multiple" size="15" ondblclick="javascript:copyToList('kczz','dxz')" style="width:90%;">
    <option value="1">会员</option>
    <option value="2">员工</option>
    <option value="3">管理员</option>
        </select></td>
        </tr>
        </table>
    </form>
    </body>
    </html>
      

  4.   

    这样就可以了
    ..............function copyToList(from,to){
      fromList = eval('document.forms[0].' + from);
      toList = eval('document.forms[0].' + to);
      if (toList.options.length > 0 && toList.options[0].value == 'temp'){
    toList.options.length = 0;
      }
      var sel = false;
      for (i=0;i<fromList.options.length;i++){
    var current = fromList.options[i];
    if (current.selected){
          for (j=0;j<toList.options.length;j++){
             var currentTo = toList.options[j];
     if(current.value == currentTo.value){
                toList.options[j] = null;
     }
      }
      sel = true;       
      txt = current.text;
      val = current.value;
      toList.options[toList.length] = new Option(txt,val);    
      fromList.options[i] = null;
      i--;
    }
       }
       if (!sel) alert ('请您选择组');
      }