日期:2014-05-16 浏览次数:20838 次
<select name="classid" id="bigclass"></select>
<select name="smallclassid" id="minclass"></select>
<script type="text/javascript">
var arr=[200002,102];//[小类ID,大类ID]
var json=[{id:101,name:'父类1',subclass:[{id:200001,name:'子类1-1'},{id:200002,name:'子类1-2'}]},{id:102,name:'父类2',subclass:[{id:200001,name:'子类2-1'},{id:200002,name:'子类2-2'},{id:200003,name:'子类2-3'}]}];//json数据由asp生成
var bigclass=document.getElementById("bigclass"),minclass=document.getElementById("minclass");
bigclass.onchange = function(){
for(var n=json.length;n--;){
if(json[n].id==this.value){
minclass.innerHTML="";
create(minclass,json[n].subclass);
break;
}
}
};
function create(obj,opt){
var op,select = arr && arr.pop();
for(var i=0,n=opt.length;i<n;i++){
op=document.createElement("option");
op.value=opt[i].id;
op.text=opt[i].name;
op.selected = select==opt[i].id;
//obj.options.add(new Option(opt[i].name,opt[i].id,select==opt[i].id)); IE不支持 尼玛
obj.options.add(op);
}
}
create(bigclass,json);
bigclass.onchange();
</script>