这次给大家带来js实现下拉框联动步骤详解,js实现下拉框联动的注意事项有哪些,下面就是实战案例,一起来看一下。
<!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=utf-8" />
<title> js下拉联动</title>
<script>
function setsecond(obj){
var val = obj.value;
if(val == 'en'){
var sec = document.getelementbyid('second');
sec.innerhtml = <option>one</option><option>two</option>;
}else{
var sec = document.getelementbyid('second');
sec.innerhtml = <option>一</option><option>二</option>;
}
}
</script>
</head>
<body>
<p>
<select id="first" onchange="setsecond(this)">
<option value="en">en</option>
<option value="zh">zh</option>
</select>
</p>
<p>
<select id="second">
</select>
</p>
</body>
</html>
使用innerhtml,ie浏览器不支持这种方法的,所以改进方法:
<!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=utf-8" />
<title>js下拉联动</title>
<script>
function setsecond(obj){
var val = obj.value;
if(val == 'en'){
var sec = document.getelementbyid('second');
sec.options[0] = new option(one,one);
sec.options[1] = new option(two,two);
}else{
var sec = document.getelementbyid('second');
sec.options[0] = new option(一,one);
sec.options[1] = new option(二,two);//可设置循环配置,也可一个一个配置
}
}
</script>
</head>
<body>
<p>
<select id="first" onchange="setsecond(this)">
<option value="en">en</option>
<option value="zh">zh</option>
</select>
</p>
<p>
<select id="second">
</select>
</p>
</body>
</html>
可以兼容火狐,ie等
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
nodejs手机访问本地服务器案例分析
vue中$emit 与$on父子兄弟组件操作详解
以上就是js实现下拉框联动步骤详解的详细内容。