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

菜鸟求助,怎么做一个选择级联,
<html>
<title>考试申请</title>
<head>
<script type="text/javascript">

 var termlist=new Array();
  termlist['第一学期']=["c#","计算机基础","电子商务概论",];
 termlist['第二学期']=["dreameaver","市场营销","网络","asp",];
function changeterm()
{
  var term=document.getElementById("selterm").value;
  var class=document.getElementById("selclass");
  class.options.length=0;
  for(var i in termlist)
  {
if (i==term)
{
for(var j in termlist[i]) 
  class.add(new option(termlist[i][j],null))
   

}
}
  
}

</script>
</head>
<body>
<select id="selterm" onChange="changeterm()" style="width:100px">
<option>选择学期</option>

</select>
<select id="selclass" style="width:100px">
<option>选择课程</option>
</select>


</body>
</html>

------解决方案--------------------
HTML code

<html>
<title>考试申请</title>
<head>

</head>
<body>
<select id="selterm" onChange="changeterm()" style="width:100px">
<option>选择学期</option>

</select>
<select id="selclass" style="width:100px">
<option>选择课程</option>
</select>
<script type="text/javascript">

    var termlist = [];
    termlist['第一学期'] = ["c#","计算机基础","电子商务概论"];
    termlist['第二学期'] = ["dreameaver","市场营销","网络","asp"];
    var tmp = [];
    var obj1 = document.getElementById('selterm');
    var obj2 = document.getElementById('selclass');
    for(var x in termlist){
        obj1.add(new Option(x, null));
        tmp.push(x);
    }
    function changeterm(){
        var l = termlist[tmp[obj1.selectedIndex - 1]];
        obj2.length = 1;
        for(var i = 0, len = l.length; i< len; i++){
            obj2.add(new Option(l[i], null));
        }
    }
    
    
    
</script>

</body>
</html>

------解决方案--------------------
HTML code
<html>
<title>考试申请</title>
<head>
<script type="text/javascript">
var termlist = new Array();
termlist['第一学期'] = ["c#","计算机基础","电子商务概论"];
termlist['第二学期'] = ["dreameaver","市场营销","网络","asp"];
function changeterm() {
    var term = document.getElementById('selterm').value;
    var cls = document.getElementById('selclass'); //class是Javascript语言保留字,不能用作变量名
    cls.options.length = 0;
    for (var i in termlist) {
        if (i == term) {
            for(var j in termlist[i]) cls.options.add(new Option(termlist[i][j], termlist[i][j]));
        }
    }
}
window.onload = function() {
    var obj = document.getElementById('selterm');
    for(var i in termlist) obj.options.add(new Option(i, i));
}
</script>
</head>
<body>
<select id="selterm" onChange="changeterm()" style="width:100px">
  <option>选择学期</option>
</select>
<select id="selclass" style="width:100px">
  <option>选择课程</option>
</select>
</body>
</html>