烦请各位高手帮我改一下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 selected>请选择</option>
<option value="1">羽毛球</option>
<option value="2">网球</option>
<option value="3">乒乓球</option>
</select>
<select name="s2">
<option value="请选择" selected>请选择</option>
</select>
</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("请选择", " ");
select2[1][0] = new Option("羽毛球拍", " ");
select2[1][1] = new Option("羽毛球线", " ");
select2[1][2] = new Option("羽毛球", " ");
select2[1][3] = new Option("羽球服装", " ");
select2[1][4] = new Option("羽球鞋袜", " ");
select2[1][5] = new Option("羽拍包", " ");
select2[1][6] = new Option("羽毛球", " ");
select2[2][0] = new Option("网球拍", " ");
select2[2][1] = new Option("网球线", " ");
select2[2][1] = new Option("网球", " ");
select2[3][0] = new Option("乒乓球拍", " ");
select2[3][1] = 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);}
temp.options[0].selected=true;
}
</script>
</body>
</html> 展开
<html>
<head>
<title>This is a test!</title>
</head>
<body>
<form name="frm">
<select name="s1" onChange="redirec(document.frm.s1.options.selectedIndex)">
<option selected>请选择</option>
<option value="1">羽毛球</option>
<option value="2">网球</option>
<option value="3">乒乓球</option>
</select>
<select name="s2">
<option value="请选择" selected>请选择</option>
</select>
</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("请选择", " ");
select2[1][0] = new Option("羽毛球拍", " ");
select2[1][1] = new Option("羽毛球线", " ");
select2[1][2] = new Option("羽毛球", " ");
select2[1][3] = new Option("羽球服装", " ");
select2[1][4] = new Option("羽球鞋袜", " ");
select2[1][5] = new Option("羽拍包", " ");
select2[1][6] = new Option("羽毛球", " ");
select2[2][0] = new Option("网球拍", " ");
select2[2][1] = new Option("网球线", " ");
select2[2][1] = new Option("网球", " ");
select2[3][0] = new Option("乒乓球拍", " ");
select2[3][1] = 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);}
temp.options[0].selected=true;
}
</script>
</body>
</html> 展开
1个回答
展开全部
函数修改为:
function redirec(x)
{
var temp = document.frm.s2;
temp.options.length=0;
for (i=0;i<select2[x].length;i++)
{
temp.options[i]=new Option(select2[x][i].text,select2[x][i].value);}
temp.options[0].selected=true;
}
上面调用函数也可以简化下:
<select name="s1" onChange="redirec(this.value)">
function redirec(x)
{
var temp = document.frm.s2;
temp.options.length=0;
for (i=0;i<select2[x].length;i++)
{
temp.options[i]=new Option(select2[x][i].text,select2[x][i].value);}
temp.options[0].selected=true;
}
上面调用函数也可以简化下:
<select name="s1" onChange="redirec(this.value)">
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询