JS实现动态添加列表框项

<table width="200" border="0" cellspacing="0">
<tr>
    <td colspan="3"><span id="selectmsg">*请选择1-3个知识点.</span></td>
</tr>
<tr>
    <td><select name="selectleft" size="10" id="selectleft">
      <option value="1">1213123123</option>
      <option value="2">2123123123</option>
      <option value="3">3213123213</option>
      <option value="4">4123213213</option>
      <option value="5">5213123213</option>
      <option value="6">6213123213</option>
    </select></td>
    <td>
        <input type="button" id="addto" class="operateBtn" value="&gt;&gt;" /><br>
        <input type="button" id="moveback" class="operateBtn" value="&lt;&lt;" />
      </td>
    <td><select name="selectright" size="10" id="selectright">
    </select></td>
</tr>
</table>
<script type="text/javascript">
var addTo       = document.getElementById("addto");
var moveBack    = document.getElementById("moveback");
var selectLeft = document.getElementById("selectleft");
var selectRight = document.getElementById("selectright");
var count   = 0;
addTo.onclick    = addOption;
moveBack.onclick = delOption;


//这个函数检验传入的值是否在有边出现过!
function hasOption(str){
    for(var i=0;i<document.getElementById("selectRight").options.length;i++){
        if(document.getElementById("selectRight").options.value==str){
        return false;
        }
    }
    return true;
}

function addOption(){
    var nowIndex    = document.getElementById("selectRight").options.length;              //右边的下一个索引
    var selectIndex = document.getElementById("selectLeft").options.selectedIndex;        //做边被选种项索引
    var selectText = document.getElementById("selectLeft").options[selectIndex].text;    //被选种项文本
    var selectValue = document.getElementById("selectLeft").options[selectIndex].value;   //被选种项值
   
    if(selectIndex!=-1){
   if(!(hasOption(selectValue))){             //如果选了一项且右边没有,执行
    if(count<3){
     var newoption   = new Option(selectText,selectValue,false,false);
     document.getElementById("selectRight").options[nowIndex] = newoption;
     count+=1;
     document.getElementById("selectmsg").innerHTML="*你还可以选择"+(3-count)+"个知识点";   
    }else
     document.getElementById("selectmsg").innerHTML="<font color=red>*温馨提示,你只能选择三项!</font>";
   }else
    document.getElementById("selectmsg").innerHTML="<font color=red>*温馨提示,不能重复选择!</font>";
    }
}
function delOption(){
    var selectIndex = document.getElementById("selectRight").options.selectedIndex;
    if(selectIndex!=-1){
     document.getElementById("selectRight").options[selectIndex] = null;                   //清空选种项
   count-=1;
   document.getElementById("selectmsg").innerHTML="*你还可以选择"+(3-count)+"个知识点";
    }
}
function hasOption(str){
var tmp=false;
for(i=0;i<count;i++){
   if(str==document.getElementById("selectRight").options[i].value){
    tmp=true;
    break;
   }
   else
    tmp=false;
}
return tmp;
}
</script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值