如何在 js文件中设置  html:select 的选项。
 我在脚本中通过ajax方法得到数据,然后                 select1= document.getElementById("selectName")
                var option1 = document.createElement("OPTION");
                select1.options.add(option1);
                option1.value=list[i];
                option1.innerText=list[i+1];添加select 的选项。   添加完了以后我想让它默认选择第一个。 因为不默认选择第一,它会是默认选择一个空白。
我使用过   select1.value=0;  和  select1.selectedIndex=0;  都不行  请教各位高手我该如何实现

解决方案 »

  1.   

    select1.options[objSelect.length] = new Option( " text " , " value" ); 
      

  2.   


    select1= document.getElementById("selectName")
    var option1 = document.createElement("OPTION");
    option1.value=list[i];
    option1.text=list[i+1];
    option1.selected=true;
    select1.options.add(option1);
      

  3.   

    或者你干脆把第一个替换掉即可select1.options[0] = new Option( " text " , " value" ); 
      

  4.   

    var yyyy=document.getElementById("year");
    var arrYear = new Array(4);
    for(i = 0; i< arrYear.length;i++){
    arrYear[i] = date.getYear()+i;
    yyyy.options.add(new Option(arrYear[i].toString(), arrYear[i].toString())); 
    }
    这样子的话select标签就会默认选择今年作为第一个。
      

  5.   

    re: 添加完了以后我想让它默认选择第一个
    if (select1.options.length>0)
    {
        select1.options[0].selected=true;
    }
      

  6.   

    不明白楼主用select1.selectedIndex=0;为什么不行,按道理是肯定行的。是不是代码其它地方有问题?
      

  7.   

    这里有个例子,有js对select的相关操作
    http://www.cnblogs.com/guodaxia/archive/2009/05/13/1456358.html
      

  8.   

    select1 = document.getElementById("selectName")
    var option1 = document.createElement("OPTION");
        option1.value = list[i];
        option1.innerText = list[i+1];
        option1.selected = "selected";select1.options[select1.options.lengths].add(option1);
      

  9.   

    参考下,看下new Option的具体用法<select id="osel"></select>
    <script type="text/javascript">
    <!--
    var arr = ["a","b","c","d"];
    window.onload = function(){
    var osel = document.getElementById("osel");
    for(var i = 0; i < arr.length; i++){
    if(i == 1){
    osel.options.add(new Option(arr[i],arr[i],false,true));
    }else{
    osel.options.add(new Option(arr[i],arr[i]));
    }

    }
    }
    //-->
    </script>
      

  10.   

    DOM编程,select的option可以看做一个options数组,对数据元素进行赋值就可以了
    select.options[i]=new Option(text,value);
    select.options[0].selected=true;
      

  11.   

    http://blog.csdn.net/fkedwgwy/archive/2010/06/22/5687029.aspx
    jquery 超级select插件 v3.2.0.0版本  支持汉字、头写拼音、英文快速定位查询的超级select插件。可方向键、tab 键快速选择。  支持 ie6.0、7.0、8.0、firefox all 版本的历史版本如下:3.2.0.0版本:http://blog.csdn.net/fkedwgwy/archive/2010/06/13/5669690.aspx2.1.0.0版本:http://code.google.com/p/omcode/downloads/detail?name=selectsearchv2.1.0.rar&can=2&q=#makechanges1.0.0.0版本:http://blog.csdn.net/fkedwgwy/archive/2010/04/15/5489789.aspx