一个js二级联动。不知道错在哪里

anheisoft 2015-08-07 02:04:27
<select name="BigClassName" onChange="changelocation(document.myform.BigClassName.options[document.myform.BigClassName.selectedIndex].value)" size="1">


=======下面是函数=========
var onecount;
subcat = new Array();
<%
count = 0
do while not rs.eof
%>
subcat[<%=count%>] = new Array("<%= trim(rs("SmallClassName"))%>","<%= trim(rs("BigClassName"))%>","<%= trim(rs("SmallClassName"))%>");
<%
count = count + 1
rs.movenext
loop
rs.close
%>
onecount=<%=count%>;

function changelocation(locationid)
{
document.myform.SmallClassName.length = 1;
var locationid=locationid;
var i;
for (i=0;i < onecount; i++)
{
if (subcat[i][1] == locationid)
{
document.myform.SmallClassName.options[document.myform.SmallClassName.length] = new Option(subcat[i][0], subcat[i][2]);
}
}
}

=====================================
Chrome js控制台报错Uncaught TypeError: Cannot read property 'undefined' of undefined regcomad2.asp:117
onchange
...全文
162 4 打赏 收藏 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cheery_an 2015-08-10
  • 打赏
  • 举报
回复
都给报错了呀,应该是"changelocation(document.myform.BigClassName.options[document.myform.BigClassName.selectedIndex].value)中的参数的问题,某个为undefine,比如document.myform.BigClassName为undefine,document.myform.BigClassName.selectedIndex就会报那个错了 可以把事件写到函数中,调试js,从大到小逐个排除
猿敲月下码 2015-08-10
  • 打赏
  • 举报
回复
四级联动,做个参考
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE>四级联动</TITLE>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <SCRIPT LANGUAGE="JavaScript">
 <!--
	// 数据源
	 var dataOneTwo = {"rows":[
		{"text":"A",value:0,"items":[{"text":"A1",value:1},{"text":"A2",value:2}]}
		,{"text":"B",value:3,"items":[{"text":"B1",value:4},{"text":"B2",value:5}]}
		,{"text":"C",value:6,"items":[{"text":"C1",value:7},{"text":"C2",value:8}]}
		]}
	 var dataTwoThree = {"rows":[
		{"text":"A1",value:1,"items":[{"text":"A11",value:10},{"text":"A12",value:11}]}
		,{"text":"A2",value:2,"items":[{"text":"A21",value:12},{"text":"A22",value:13}]}
		,{"text":"B1",value:4,"items":[{"text":"B11",value:14},{"text":"B12",value:15}]}
		,{"text":"B2",value:5,"items":[{"text":"B21",value:16},{"text":"B22",value:17}]}
		]}
	 var dataThreeFour = {"rows":[
		{"text":"A11",value:10,"items":[{"text":"0",value:18}]}
		,{"text":"A12",value:11,"items":[{"text":"2",value:19}]}
		,{"text":"B11",value:14,"items":[{"text":"4",value:20}]}
		,{"text":"B12",value:15,"items":[{"text":"6",value:21}]}

		,{"text":"A21",value:12,"items":[{"text":"1",value:22}]}
		,{"text":"A22",value:13,"items":[{"text":"3",value:23}]}
		,{"text":"B21",value:16,"items":[{"text":"5",value:24}]}
		,{"text":"B22",value:17,"items":[{"text":"7",value:25}]}
		]}


	window.onload=function(){
		bindData("one",dataOneTwo.rows);
		bindEvent();
	}
	
	// 绑定数据
	function bindData(selDomId,items){
		var sel = document.getElementById(selDomId);	
		var opts = sel.options;
		sel.innerHTML = '';
		opts.add(new Option("--请选择--",""))
		for(var i=0,len=items.length;i<len;i++){
			opts.add(new Option(items[i].text,items[i].value))
		}	
	}
	
	function bindEvent(){
		bindSelectEvent("one",dataOneTwo,"two");
		bindSelectEvent("two",dataTwoThree,"three");
		bindSelectEvent("three",dataThreeFour,"four");
	}	

	// 绑定select事件
	function bindSelectEvent(domId,srcData,childId){
		var sel = document.getElementById(domId);
		sel.onchange=function(){
			var items = findChild(this,srcData);
			bindData(childId,items);
		}
	}	

	// 找到子节点数据
	function findChild(dom,srcData){
		var rows = srcData.rows;
		var value = dom.value;
		for(var i=0,len=rows.length;i<len;i++){
			if(value == rows[i].value){
				return rows[i].items;
			}
		}
		return [];
	}
 //-->
 </SCRIPT>

 </HEAD>
 <BODY>
  <select id="one"  name=one> </select> 
<select id="two"  name=two> </select> 
<select id="three"  name=three> </select> 
<select id="four"  name=four> </select> 
 </BODY>
</HTML>
hch126163 2015-08-09
  • 打赏
  • 举报
回复
1、 右键,查看源代码,看看拼接的js 代码是否有错 2、单步 调试你的js代码
zhanbaone 2015-08-07
  • 打赏
  • 举报
回复
http://www.jb51.net/article/26947.htm 可以去看看 兄弟

87,984

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧