var province = document.getElementById("Select1");
        var hid1 = document.getElementById('<%=HiddenField4.ClientID %>');
        for(var i = 0; i < province.options.length;i++)
        {
            if(province.options[i].value  == hid1.value)
            { 
                province.options[i].selected = true; 
            }
        }   这段代码在IE中执行就出现"无法设置selected属性。未指明的错误." 在IE7中正常.
  哪位牛人帮忙改改。。

解决方案 »

  1.   

    试下
    var province = document.getElementById("Select1");
            var hid1 = document.getElementById(' <%=HiddenField4.ClientID %>');
            for(var i = 0; i < province.options.length;i++)
            {
                if(province.options[i].value  == hid1.value)
                {
                    province.options[i].setAttribute("selected","selected");
                }
            } 
      

  2.   


    var province = document.getElementById("Select1");
            var hid1 = document.getElementById(' <%=HiddenField4.ClientID %>');
            for(var i = 0; i < province.options.length;i++)
            {
                if(province.options[i].value  == hid1.value)
                {
                    province.options[i].setAttribute("selected","selected");
    break;
                }
            }
      

  3.   

      试了,不报错,但是也加载不出select的值。
      

  4.   

    IE6测试正常,楼主应该排查是不是其他地方导致的。
    如果问题依旧出现,请提供网页链接,我用IE6访问试试。<select id="Select1">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    </select>
    <input id="hid1" type="hidden" value="3"/>
    <script type="text/javascript">
    var province = document.getElementById("Select1");
    var hid1 = document.getElementById('hid1');
    for (var i = 0; i < province.options.length; i++) {
    if (province.options[i].value == hid1.value) {
    province.options[i].selected = true;
    break;
    }
    }
    </script>
      

  5.   

     没有链接。。但是我IE7为什么运行正常的,我是做一个修改的页面,3个SELECT控件,及:省,市,县
     但是楼上的方法,在IE6中加载不出,我IE7就很正常。还有一点奇怪,就是省能加载出来,市 县到了IE6就没了
      

  6.   

    你可以试试放在onload中执行document.onload = function() {
        // todo
    }
      

  7.   

    费劲你提供完整的html源文件(浏览器右键查看源文件),先让我们能看到你说的现象。
      

  8.   

    要不要我给你看我整个前台的javascript 代码。?
      

  9.   

       var xmlhttp;
        //创建XMLhttpRequest
         function CreateXmlhttp()
         {
              try
              {
                   if(window.ActiveXObject)
                   {
                        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                   }
                   else
                   {
                        xmlhttp=new XMLHttpRequest();
                   }
              }
              catch(Error)
              {
                throw new Error("无法创建xmlHttpRequest");
              }
         }
         //获取省
          window.onload=function(){
           CreateXmlhttp();
           xmlhttp.open("post","Help.aspx?action=true",true);
           xmlhttp.onreadystatechange=SelectProvince;
           xmlhttp.send(null);
          }
          //省的回调函数
          function SelectProvince()
          {       
                if(xmlhttp.readyState==4 || xmlhttp.readyState=="Complete")
                {
                    var ContrPrice=xmlhttp.responseText;
                    var arry=new Array();
                    arry=ContrPrice.split("/");
                    var text=new Array();
                    var values=new Array();
                    text=arry[1].split(",");
                    values=arry[0].split(",");
                     var province=document.getElementById("Select1");
                   for(var i=0;i<text.length-1;i++)
                    {   
                        var option=document.createElement("option");
                        option.innerHTML=text[i];
                        option.value=values[i];
                        province.appendChild(option);
                    }
                      SetValue3();
                }
          }
           //获取市
          function Selectcity()
          {
                var PriceId=document.getElementById("Select1").value;
                CreateXmlhttp();
                xmlhttp.open("post","Help.aspx?province_id="+PriceId,true);
                xmlhttp.onreadystatechange=cityName;
                xmlhttp.send(null);
                var city=document.getElementById("Select2");
                for(var i=1;i<city.length;)
                {
                    city.remove(i);
                }
                 var  district =document.getElementById("Select3");
                for(var i=1;i<district.length;)
                {
                    district.remove(i);
                }
                
          }
          //市的回调函数
           function cityName()
           {
                if(xmlhttp.readyState==4 || xmlhttp.readyState=="Complete")
                {
                     var City=xmlhttp.responseText; 
                     var grrn= City.split("/"); 
                     var cityText=new Array();
                     var CityValue=new Array();
                     cityText=grrn[1].split(",");
                     CityValue=grrn[0].split(",");   
                     var city=document.getElementById("Select2");
                     
                      for(var i = 0 ; i < city.length ; i++)    //填充前前先清空
                {
           if(i != 0)
            {
                city.remove(i);
                i--;
            }
            }  
                     for(var i=0;i <cityText.length-1;i++)
                     {
                     
                        var option=document.createElement("option");
                        option.innerHTML=cityText[i];
                        option.value=CityValue[i];
                        city.appendChild(option);
                     }
                       SetValue4();
                }
           }
          //获取县
          function SelectCounty()
          {
               CreateXmlhttp();
               var City_id=document.getElementById("Select2").value; 
               xmlhttp.open("post","Help.aspx?City_id="+City_id);
               xmlhttp.onreadystatechange=County;
               xmlhttp.send(null); 
              
          } 
          //县的回调函数
          function County()
          {
             if(xmlhttp.readyState==4 || xmlhttp.readyState=="Complete")
             {
                 var County=xmlhttp.responseText;
                var grrn=County.split("/");
                var Count_Text=grrn[0].split(",");
                var Count_value=grrn[1].split(",");
                 var Count_post=grrn[2].split(",");
                var County_select=document.getElementById("Select3");
                for(var i = 0 ; i < County_select.length ; i++)    //填充前前先清空
        {
    if(i != 0)
    {
        County_select.remove(i);
        i--;
    }
    }  
                for(var i=0;i<Count_Text.length-1;i++)
                {
                   var option= document.createElement("option");
                    option.innerHTML=Count_Text[i];
                    option.value=Count_value[i];
                    County_select.appendChild(option);
                }
                  var text = document.getElementById('<%=txtPost.ClientID %>');
                 text.value =  Count_post[0];
                 SetValue5();
             }
          }
          
         //加载页面时设置select的值
        function SetValue3()
        {
            var province = document.getElementById("Select1");
            var hid1 = document.getElementById('<%=HiddenField4.ClientID %>');
            for(var i = 0; i < province.options.length;i++)
            {
                if(province.options[i].value  == hid1.value)
                { 
                 province.options[i].selected = true;
                // break;
                }
            }
             Selectcity();
        }
         function SetValue4()
         {
             var city = document.getElementById("Select2");
            var hid2 = document.getElementById('<%=HiddenField5.ClientID %>');
            for(var i = 0; i < city.options.length;i++)
            {
                if (city.options[i].value == hid2.value)
                { 
                 city.options[i].selected = true; 
                }
            } 
           
           SelectCounty();
         }
         function SetValue5()
         {
            var district = document.getElementById("Select3");
            var hid3 = document.getElementById('<%=HiddenField6.ClientID %>');
            for(var i = 0; i < district.options.length;i++)
            {
                if (district.options[i].value  == hid3.value)
                { 
                 district.options[i].selected = true;
                } 
            }
         }
      

  10.   

    HiddenField的值是后台赋值的  可以拿到的 我测试过的。。
      

  11.   

    挺复杂,要运行还得写个后台先在关键几个地方alert()一下,看看一些参数是不是理想的和顺序是不是期望的Good Luck
      

  12.   

    怪了 我加2个alert()  它就出来了。。
      

  13.   

    if (province.options[i].value == hid1.value) {
                province.value = province.options[i].value;
                break;
            }
      

  14.   

     楼上的大哥。你的和我的开始一样,要加
                if(province.options[i].value  == hid1.value)
                { 
                alert(hid1.value);           province.value = province.options[i].value; 
                break; 
                }
    才能加载出来
      

  15.   

    那事情算是有进展了你可以考虑在alert所处的函数延迟调用比如:
    setTimeout(function () {
         ...........
    }, 100);
      

  16.   

    把对应的option[要选中的selectedIndex].value赋值为你要选中的那个的value .这样也照样可以选中
      

  17.   

    var province = document.getElementById("Select1");
            var hid1 = document.getElementById(' <%=HiddenField4.ClientID %>');
            for(var i = 0; i < province.options.length;i++)
            {
                if(province.options[i].value  == hid1.value)
                {
                    province.options[i].value= hid1.value;
                }
            }
    -----------------
    这样试试
      

  18.   

    province.options[i].value= hid1.value; 
    break; //再加个退的
      

  19.   

    首先可以在添加option的同时设置:if(i == 4){ op.selected = true; }
    还可以用setTimeout延迟一下:setTimeout(function(){ oSelect.selectedIndex = iIndex; }, 0);
    最后是比较推荐的用setAttribute来设置:oSelect.options[iIndex].setAttribute("selected", "true");参考这里以前测试的,不知对不对
      

  20.   


    刚刚卸载了IE7。这样处理: if(province.options[i].value  == hid1.value)
                { 
                setTimeout(function(){ province.options[i].selected = true;}, 1);
               
                }  province.options[i]为空或不是对象。。
      

  21.   

     上面的方法都试了,能用的都必须在方法执行前加  alert("dd")
    if(province.options[i].value  == hid1.value)
                { 
                  alert("dd");
             province.options[i].setAttribute("selected", "true"); 
                }
     哪个没加 哪个就不会出来
      怎么回事?
      

  22.   

    for (var i = 0; i < province.options.length; i++) {
        (function(){
        if (province.options[i].value == hid1.value) {
            
            province.options[i].selected=true;
                return false;
            }
        })()
            
        }
      

  23.   

     我觉得alert能出来,正常加载就不出来。 就找了我的 SetValue3(),和 Selectcity(); 
    这2个方法一起是相互调用的。。嘿嘿、 在Selectcity()方法里写了个第定时器 setTimeout("SetValue3()",1) ; 谢谢各位大哥了 结账。
      

  24.   

    http://topic.csdn.net/u/20091104/14/baaaa29e-f0b5-4b31-adfe-5db9514dcfea.html
     谁去帮我看看这个啊
      又是一个IE6的问题
      

  25.   

    改成:
    try{
        province.options[i].selected = true;  
    }catch(e){}