js动态加载下拉框里的值

来源:互联网 发布:如何做网络优化 编辑:程序博客网 时间:2024/06/06 16:07
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script type="text/javascript">
function bbb()
{
    var s1 = document.getElementById("Select2");
    var o = s1.getAttribute("value");
    var cbo = document.getElementById("select1");
    for(var i=0;i<cbo.options.length;i++)
    {
        cbo.options.remove(i);
    }
    if(o=="1")
    {
        var o1 = document.createElement("option");
        o1.innerHTML = "abc";
        o1.setAttribute("value","1");
        var o2 = document.createElement("option");
        o2.innerHTML = "abc1";
        o2.setAttribute("value","3");
        var o3 = document.createElement("option");
        o3.innerHTML = "abc2";
        o3.setAttribute("value","2");
        var s2 = document.getElementById("select1");
        s2.appendChild(o1);
        s2.appendChild(o2);
        s2.appendChild(o3);
    }
    else
    {
        cbo.options.remove(0);
    }
}
</script>
<body onload="bbb();">
<form id="form1" name="form1" method="post" action="">
  <label>
  <select name="select" id="select1">
  </select>
  </label> <input type="button" value="asd" onclick="bbb();" style="width: 57px" />
  <select id="Select2" onchange="bbb();" name="D1">
  <option value="1">A</option>
  <option value="2">B</option>
  </select></form>
</body>
</html>
原创粉丝点击