`
simpledev
  • 浏览: 194235 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

JS动态添加select下拉框(城市区域多级下拉框demo)

    博客分类:
  • js
 
阅读更多

动态获取select中的options数量:

        Var size = document.getElementById("ddlResourceType").options.length;

动态删除select中的所有options
       document.getElementById("ddlResourceType").options.length=0;

动态删除select中的某一项option
       document.getElementById("ddlResourceType").options.remove(indx); 

动态添加select中的项option:
document.getElementById("ddlResourceType").options.add(new Option(text,value));

上面在IEFireFox都能测试成功,希望以后你可以用上。

 

其实用标准的DOM操作也可以,就是document.createElementappendChildremoveChild之类的。

取值方面
    function getvalue(obj)
    {
        var m=obj.options[obj.selectedIndex].value
        alert(m);//
获取value
        var n=obj.options[obj.selectedIndex].text
        alert(n);//
获取文本
    }

==============================================================================
1
检测是否有选中
if (objSelect.selectedIndex > - 1 ) {
//
说明选中
} else {
//
说明没有选中
}

option设为选中:document.getElementById("province").options.selected = true;

2 删除被选中的项
objSelect.options[objSelect.selectedIndex] = null ;

3 增加项
objSelect.options[objSelect.length] = new Option( "
你好 " , " hello " );

4 修改所选择中的项
objSelect.options[objSelect.selectedIndex] = new Option( "
你好 " , " hello " );

5 得到所选择项的文本
objSelect.options[objSelect.selectedIndex].text;

6 得到所选择项的值

objSelect.options[objSelect.selectedIndex].value;

7.所选择项设置为选中

objSelect.options[objSelect.selectedIndex].selected = true;

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>城市区域4级下拉框</title>
<script>
	function f1(v){
		//alert(v);
		//清除后面的项
		document.getElementById("object2").options.length=0
		document.getElementById("object3").options.length=0
		document.getElementById("object2").style.display="none";
		document.getElementById("object3").style.display="none";
		
		
		var s  =  document.getElementById("object1");   
		if(v=="北京"){
			var option0  =   new  Option("东城区","a1");
			var option1  =   new  Option("西城区","a2");
			var option2  =   new  Option("朝阳区","a3");
			s.options[0] = option0;
			s.options[1] = option1;
			s.options[2] = option2;
		}else if(v=="上海"){
			var option0  =   new  Option("浦东新区","b1");
			var option1  =   new  Option("闵行区","b2");
			var option2  =   new  Option("徐汇区","b3");
			s.options[0] = option0;
			s.options[1] = option1;
			s.options[2] = option2;
			s.options[2].selected = true;
			
		}else if(v=="广州"){
			var option0  =   new  Option("天河区","c1");
			var option1  =   new  Option("越秀区","c2");
			var option2  =   new  Option("海珠区","c3");
			s.options[0] = option0;
			s.options[1] = option1;
			s.options[2] = option2;
		}
		//显示出来
		document.getElementById("object1").style.display="";
	}
	
	function f2(v){
		//alert(v);
		//清除后面的项
		document.getElementById("object3").options.length=0
		document.getElementById("object3").style.display="none";
		
		var s  =  document.getElementById("object2");   
		if(v=="b1"){
			var option0  =   new  Option("陆家嘴","a1");
			var option1  =   new  Option("东方明珠","a2");
			s.options[0] = option0;
			s.options[1] = option1;
		}else if(v=="b2"){
			var option0  =   new  Option("浦江镇","b1");
			var option1  =   new  Option("梅陇镇","b2");
			s.options[0] = option0;
			s.options[1] = option1;
		}else if(v=="b3"){
			var option0  =   new  Option("田林镇","c1");
			var option1  =   new  Option("徐家汇","c2");
			s.options[0] = option0;
			s.options[1] = option1;
		}
		//显示出来
		document.getElementById("object2").style.display="";
	}
	
	function f3(v){
		//alert(v);
		var s  =  document.getElementById("object3");   
		if(v=="c1"){
			var option0  =   new  Option("田林路","a1");
			var option1  =   new  Option("漕宝路","a2");
			s.options[0] = option0;
			s.options[1] = option1;
		}else if(v=="c2"){
			var option0  =   new  Option("肇嘉浜路","b1");
			var option1  =   new  Option("华山路","b2");
			s.options[0] = option0;
			s.options[1] = option1;
		}
		//显示出来
		document.getElementById("object3").style.display="";
	}
</script>
</head>

<body>
<select name="select1" onChange="f1(this.value);">
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="广州">广州</option>
</select>
<select id="object1" name="select2" onChange="f2(this.value);" style="display:none ">
</select>
<select id="object2" name="select3" onChange="f3(this.value);" style="display:none ">
</select>
<select id="object3" name="select4" style="display:none ">
</select>
</body>
</html>

 

 

 

分享到:
评论
2 楼 qq672076266 2013-09-18  
qq672076266 写道

终于找到一个好用的了。
其他的都是原版抄去抄去,真服了.............

抄来抄去
1 楼 qq672076266 2013-09-18  

终于找到一个好用的了。
其他的都是原版抄去抄去,真服了.............

相关推荐

Global site tag (gtag.js) - Google Analytics