您的位置:首页 > Web前端 > JavaScript

js 动态生成下拉列表 下拉列表级联

2012-02-02 14:17 639 查看
简单的动态生成下拉列表 并且 带有级联操作

根据父级节点的值来组织子级下拉列表的值

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function changeF(fatherValue)
{
	if(fatherValue!=null)
	{
		var s2 = document.getElementById('id2');
		//s2.length=1;
		s2.length = 1;//保留下拉列表的第一条,如果将所有的都清楚了,则值等于0即可
		for(var i=0;i<3;i++)
		{
			//alert(i);
			var v = document.createElement('option');
			v.setAttribute('value', i);
			var inner = document.createTextNode("我的值是_"+i+"_fatherValue="+fatherValue);	
			v.appendChild(inner);
			s2.appendChild(v);
		}
		
	}
	
}
</script>
</head>

<body>
	父级下拉列表:
	<select id="id1" name="id1" onchange="changeF(this.value);">
		<option value="1">1</option>
		<option value="2">2</option>
		<option value="3">3</option>
	</select>
	子级下拉列表:
	<select id="id2" name="id2" onchange="javascript:alert(this.value);">
		<option value="-1">ok</option>
	</select>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: