代码驿站移动版
频道导航
HTML/Xhtml
CSS
JavaScript
HTML5
PHP教程
ASP.NET
正则表达式
AJAX
ThinkPHP
Yii
MySQL
MariaDB
Oracle
MongoDB
Redis
DedeCMS
PHPCMS
帝国CMS
WordPress
Discuz
其它CMS
Zend Studio
Sublime
Notepad
Dreamweaver
Windows
Linux
Nginx
Apache
IIS
CentOS
Ubuntu
Debian
网站优化
工具资源
PHP源码
ASP.NET源码
其它源码
图标素材
按钮素材
字体素材
DedeCMS模板
帝国CMS模板
PHPCMS模板
WordPress模板
Discuz!模板
单页模板
开发软件下载
服务器软件下载
广告投放
联系我们
版权申明
软件编程
网页前端
移动开发
数据库
服务器
脚本语言
PHP代码
JAVA代码
Python代码
Android代码
当前位置:
主页
>
网页前端
>
JavaScript代码
>
JS 动态添加列表框项效果代码
时间:2021-05-21 08:28:27 | 栏目:
JavaScript代码
| 点击:次
<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=">>" /> <input type="button" id="moveback" class="operateBtn" value="<<" /> </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>
[Ctrl+A 全选 注:
引入外部Js需再刷新一下页面才能执行
]
您可能感兴趣的文章:
JavaScript常见事件处理程序实例总结
Javascript的匿名函数小结
url传递的参数值中包含&时,url自动截断问题的解决方法
Javascript别踩白块儿(钢琴块儿)小游戏实现代码
兼容浏览器的js事件绑定函数(详解)
相关文章
10-19
javascript上下左右定时滚动插件
11-23
Webpack中loader打包各种文件的方法实例
11-27
Javascript实例教程(19) 使用HoTMetal(2)
10-05
javascript判断图片是否加载完成的方法推荐
11-22
在javascript中执行任意html代码的方法示例解读
JQuery
VUE
AngularJS
MSSql
MySQL
MongoDB
Redis
Linux
Tomcat
Nginx
网站首页
广告投放
联系我们
版权申明
联系站长