+ -
当前位置:首页 → 问答吧 → 向下拉列表框中动态添加option不成功!

向下拉列表框中动态添加option不成功!

时间:2010-08-17

来源:互联网

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


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

作者: blandyzld   发布时间: 2010-08-17

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>

作者: hookee   发布时间: 2010-08-17

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>


W3C标准中的方法描述
selectObject.add(option,before)
参数 描述
option 必需。要添加选项元素。必需是 option 或 optgroup 元素。
before 必需。在选项数组的该元素之前增加新的元素。如果该参数是null,元素添加到选项数组的末尾。

而在低版本的IE中支持 selectObject.add(option)

作者: WebAdvocate   发布时间: 2010-08-17