代码驿站移动版
频道导航
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代码
>
关联的Select,可以支持客户端动态更新
时间:2020-12-30 14:32:47 | 栏目:
JavaScript代码
| 点击:次
这是我很久就写好了的一个小程序。关联的Select,前后的select都是关联的,前面的select影响到后面的内容,为了防止服务器的重新读的负担,我加入了客户端自动更新功能,这样每次更新都不必再读取数据库而刷新页面。
虽然是为了我现在正在做的一个系统而设计的(可以运用到数据库里去),但是它的实现理论应该有人会用得到吧。
现在我放出来让大家研究,研究。也欢迎大家指出不足的地方。
点击下载此原代码
演示(演示无法添加数据,请下载原代码)
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>Test</title> <script> var fy=new Array() var fl=new Array() var mx=new Array() var lb=new Array() var sendvalue=new openvalue("","","") var tArray=new Array("fy","fl","mx","lb") //--主类别结构-- function mainArray(fyid,text) { this.fyid=fyid this.text=text } //--关联类别结构-- function childArray(id,fatherid,text) { this.id=id this.fatherid=fatherid this.text=text } function openvalue(whatdo,fid,ftext) { this.whatdo=whatdo this.fid=fid this.ftext=ftext } //--添加数组-- function addArray(id,fatherid,text,ArrayName,Type) { var AN=eval(ArrayName) if (Type==0) { AN[AN.length]=new mainArray(id,text) } if (Type==1) { AN[AN.length]=new childArray(id,fatherid,text) } } //--添加数据到select-- function addSelect(value,text,sel) { var selectbox=eval("document.forms[0]."+sel) var newoption=document.createElement("OPTION"); selectbox.add(newoption) if (value=="add") { newoption.text=text selectbox.options[selectbox.length-1].style.backgroundColor="#FFEEEE" } else { newoption.text=" "+text } newoption.value=value } //--从数组里读出数据-- function loadSelect(ArrayName,Type,fatherid) { removeall(ArrayName) var AN=eval(ArrayName) switch (Type){ case 0: for (i=0;i<AN.length;i++) { addSelect(AN[i].fyid,AN[i].text,ArrayName) } addSelect("add","<添加数据>",ArrayName) break; case 1: for (i=0;i<AN.length;i++) { if (AN[i].fatherid==fatherid) { addSelect(AN[i].id,AN[i].text,ArrayName) } } addSelect("add","<添加数据>",ArrayName) break; } } //--删除所有选项-- function removeall(sel) { var selectbox=eval("document.forms[0]."+sel) selectbox.options.length=1 selectbox.options[0].selected } //--改变选择时-- function change_select(c_sel){ for (i=0;i<tArray.length;i++) { if (tArray[i]==c_sel) { if (i>0) {var lastsel=tArray[i-1]} var nowsel=tArray[i] if (i<tArray.length) {var nextsel=tArray[i+1]} } } if (lastsel) {last_sel=eval("document.forms[0]."+lastsel)} now_sel=eval("document.forms[0]."+nowsel) if (nextsel) {next_sel=eval("document.forms[0]."+nextsel)} if (now_sel.value=="add") { if (lastsel) { if (last_sel.selectedIndex==0 || last_sel.selectedIndex==last_sel.length-1) {alert("选择继承对象");return} showpop("pop.htm",nowsel,last_sel.value,last_sel.options[last_sel.selectedIndex].text) } else { showpop("pop.htm",nowsel) } return } if (now_sel.value=="null") { del=false for (i=0;i<tArray.length;i++) { if (tArray[i]==nextsel) del=true if (del) {removeall(tArray[i]) } } } else { if (nextsel) loadSelect(nextsel,1,now_sel.value) } } //--打开新窗口-- function showpop(url,id,fatherid,fathertext){ sendvalue=new openvalue(id,fatherid,fathertext) var width=300; var height=140; var w = 1024; var h = 768; if (document.all || document.layers) { w = screen.availWidth; h = screen.availHeight; } var leftPos = (w/2-width/2); var topPos = (h/2.3-height/2.3); var pop=window.open(url,"","width="+width+",height="+height+",top="+topPos+",left="+leftPos) } </script> </head> <script> //--添加费用类别-- addArray("1","","工程费用","fy",0) addArray("2","","固定费用","fy",0) //--添加分类-- addArray("1","1","包工包料","fl",1) addArray("2","1","人工","fl",1) addArray("3","1","材料","fl",1) addArray("4","1","运杂费","fl",1) addArray("5","1","消费城管","fl",1) addArray("6","1","管理费用","fl",1) addArray("7","1","税费","fl",1) addArray("8","1","交际费","fl",1) addArray("9","2","工资","fl",1) addArray("10","2","房租","fl",1) addArray("11","2","水电","fl",1) addArray("12","2","电话","fl",1) addArray("13","2","办公用品","fl",1) addArray("14","2","广告","fl",1) //--添加明细-- addArray("1","1","铝合金","mx",1) addArray("2","1","不锈钢","mx",1) addArray("3","1","家具","mx",1) addArray("4","2","木工","mx",1) addArray("5","2","泥工","mx",1) addArray("6","2","水工","mx",1) //--添加材料类别-- addArray("1","1","铝合金1","lb",1) addArray("2","1","铝合金2","lb",1) addArray("3","1","铝合金3","lb",1) function pageload(){ loadSelect("fy",0) } </script> <body onload=pageload()> <b>演示中(演示无法添加数据,请下载原代码)</b> <form name="frm"> <p> <select size="1" name="fy" onchange='change_select("fy")'> <option value="null" style="background-color:#EEEEFF" selected>选择类别--</option> <option value="add" style="background-color:#FFEEEE"><添加数据></option> </select> <select size="1" name="fl" onchange='change_select("fl")'> <option value="null" style="background-color:#EEEEFF" selected>选择分类--</option> </select> <select size="1" name="mx" onchange='change_select("mx")'> <option value="null" style="background-color:#EEEEFF" selected>选择明细--</option> </select> <select size="1" name="lb" onchange='change_select("lb")'> <option value="null" style="background-color:#EEEEFF" selected>选择材料类别--</option> </select> <input type="button" value="按钮" name="B1"></p> </form> </body> </html>
[Ctrl+A 全选 注:
引入外部Js需再刷新一下页面才能执行
]
您可能感兴趣的文章:
JavaScript常见事件处理程序实例总结
url传递的参数值中包含&时,url自动截断问题的解决方法
Javascript的匿名函数小结
javascript上下左右定时滚动插件
Javascript别踩白块儿(钢琴块儿)小游戏实现代码
相关文章
11-08
兼容浏览器的js事件绑定函数(详解)
11-27
Javascript实例教程(19) 使用HoTMetal(2)
11-23
Webpack中loader打包各种文件的方法实例
10-05
javascript判断图片是否加载完成的方法推荐
11-22
在javascript中执行任意html代码的方法示例解读
JQuery
VUE
AngularJS
MSSql
MySQL
MongoDB
Redis
Linux
Tomcat
Nginx
网站首页
广告投放
联系我们
版权申明
联系站长