js怎么样用onchange动态创建一个新的二维数组,
1个回答
展开全部
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>RunJS 演示代码</title>
<script>
var data1=[
"北京","河北","山西"
];
var data2=[
["朝阳","海淀"],
["石家庄","邯郸"],
["太原","大同"]
];
onload=function(){
var s1=document.getElementById("s1");
var s2=document.getElementById("s2");
for(var i=0;i<data1.length;i++){
var opt=document.createElement("option");
opt.innerHTML=data1[i];
opt.value=i;
s1.appendChild(opt);
}
s1.onchange=function(){
while(s2.children.length!=0){
s2.removeChild(s2.children[0]);
}
var brr=data2[this.value];
if(null==brr){
var opt=document.createElement("option");
opt.innerHTML="请选择";
s2.appendChild(opt);
}else{
for(var i=0;i<brr.length;i++){
var opt=document.createElement("option");
opt.innerHTML=brr[i];
opt.value=i;
s2.appendChild(opt);
}
}
}
}
</script>
</head>
<body>
<select id="s1">
<option>请选择</option>
</select>
<select id="s2">
<option>请选择</option>
</select>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询