日期:2014-05-16  浏览次数:20384 次

关于JS 联动,下拉框选项前后选择个数不同
 或者直接看后面的问题 我向贴上我的代码,各位可运行一下
<html> 
<head> 
<title>This is a test!</title> 
</head> 
<body> 
<form name="frm"> 
<select name="s1" onChange="redirec(document.frm.s1.options.selectedIndex)"> 
<option value="id" selected>学号</option> 
<option value="name">姓名</option> 
<option value="age">年龄</option> 
</select> 
<select name="s2"> 

</select> 
<input type="submit" value="查询"/>
</form> 
<script language="javascript"> 
//获取一级菜单长度 
var select1_len = document.frm.s1.options.length; 
var select2 = new Array(select1_len); 
//把一级菜单都设为数组 
for(i=0; i<select1_len; i++) 

select2[i] = new Array();

//定义基本选项 

select2[0][0] = new Option("包含", "contain"); 
select2[0][1] = new Option("等于", "equal"); 

select2[1][0] = new Option("包含", "contain"); 
select2[1][1] = new Option("等于", "equal"); 


select2[2][0] = new Option(">", ">"); 
select2[2][1] = new Option("=", "="); 
select2[2][2] = new Option("<", "<"); 
select2[2][3] = new Option("<=", "<=");
select2[2][4] = new Option(">=", ">="); 
//联动函数 
function redirec(x) 


var temp = document.frm.s2; 
for (i=0;i<select2[x].length;i++) 
{
temp.options[i]=new Option(select2[x][i].text,select2[x][i].value);

alert("x:"+x+"  select2["+x+"].length:"+select2[x].length);
temp.options[0].selected=true; 

</script> 
</body> 
</html>


我把我的问题的截图贴出来:
1、先选这个



2、在选这个



3、最后选择呢 学号,下拉框多呢几个。  多余的下拉选项是怎么产生的,求解决之道。 


?
js联动 js联动下拉框选项不一致

------解决方案--------------------
<html> 
<head> 
    <title>This is a test!</title> 
</head> 
<body> 
<form name="frm"> 
    <select name="s1" onChange="redirec(document.frm.s1.options.selectedIndex)">