日期:2014-05-17  浏览次数:20998 次

向下拉列表框中动态添加option不成功!
<html>
<head>
<script type="text/javascript">
function GenSelectListEx()
{
  var curobj=document.getElementById("id_video_format");
   
for(var i = 0; i < 5; i++)
  {
var optionelement = document.createElement("option");
//optionelement.style.cssText ="value:"options[i].optionValue";";
optionelement.text="1111";
curobj.add(optionelement,null);
//var textnode = document.createTextNode(options[i].optionName);
//optionelement.appendChild(textnode);
//curobj.appendChild(optionelement);
  //curobj.innerHTML=selectelement.innerHTML;
  }
   
}

</script>
</head>
<body>
  <div id="my_div" style="width: 750px; margin-top: 10px; text-align: left; overflow: hidden;">
   
  <select name="video_format" class="td_select" id="id_video_format">
   
  </select>
   
   
  </div>

  <script type="text/javascript">
   
  GenSelectListEx();
  </script>

</body>
</html>


为什么我这样做总是提示错误,不知道什么原因!请大家指点。

------解决方案--------------------
HTML code

<html>
<head>
<script type="text/javascript">
function GenSelectListEx()
{
  var curobj=document.getElementById("id_video_format");
  for(var i = 0; i < 5; i++)
  {
    var optionelement = new Option("111", "111");
    curobj.options.add(optionelement);
  }
}
window.onload=function(){
  GenSelectListEx();
}
</script>
</head>
<body>
  <div id="my_div" style="width: 750px; margin-top: 10px; text-align: left; overflow: hidden;">
      <select name="video_format" class="td_select" id="id_video_format">
      </select>
  </div>
</body>
</html>

------解决方案--------------------
HTML code
<html>
<head>
    <script type="text/javascript">
        function GenSelectListEx() {
            var curobj = document.getElementById("id_video_format");
            for (var i = 0; i < 5; i++) {
                var optionelement = document.createElement("option");
                //optionelement.style.cssText ="value:"options[i].optionValue";";
                optionelement.text = "1111";
                try {
                    curobj.add(optionelement, null);
                } catch(e) {
                    curobj.add(optionelement);
                }
            }
        }
    </script>
</head>
<body>
<div id="my_div" style="width: 750px; margin-top: 10px; text-align: left; overflow: hidden;">
    <select name="video_format" class="td_select" id="id_video_format">
    </select>
</div>
<script type="text/javascript">
    GenSelectListEx();
</script>
</body>
</html>