用innerHTML实现二级联动菜单

最近小编我在做二级联动菜单的时候,上网查了一些资料,发现他们用的是二位数组,发现他们写的有点麻烦,对于小编我这种初入JS的小白来说,好费力。所以自己就琢磨着想通过获取select标签中value值来相应的往另一个select标签中插入option标签不就行了么?果然没问题!代码附上:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
	<script>
		
		function change(){	var myselect=document.getElementById('sel');
			
			var index=myselect.selectedIndex;
			
			var val=myselect.options[index].value;
			
			if(3==val){document.getElementById("rrr").innerHTML="<select><option value='1' >合肥</option><option value='2'>安庆</option><option value='3' selected>黄山</option><select>";}
			}
		
	</script>
</head>
<body>
	<select name="sel" id="sel" onChange="return change();">
		<option value="1" >吉林</option>
		<option value="2">长春</option>
		<option value="3" selected>安徽</option>
	</select>
	<select id="rrr"></select>
 
</body>
</html>

顺便说一下,我是通过firefox测试的,这说明在firefox里是可以通过属性inner HTML往select标签里插入option标签

猜你喜欢

转载自blog.csdn.net/de_debug/article/details/81137835
今日推荐