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

来源:互联网 发布:mac会变的吗 编辑:程序博客网 时间:2024/06/10 22:09

简单的动态生成下拉列表 并且 带有级联操作

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

<!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>


 

原创粉丝点击