首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

IE6<select>有关问题: "无法设置selected属性。未指明的异常。”

2012-07-31 
IE6select问题: 无法设置selected属性。未指明的错误。”var province document.getElementById(Select

IE6<select>问题: "无法设置selected属性。未指明的错误。”
 
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中正常.
  哪位牛人帮忙改改。。

[解决办法]

JScript code
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;            }        }
[解决办法]
IE6测试正常,楼主应该排查是不是其他地方导致的。
如果问题依旧出现,请提供网页链接,我用IE6访问试试。

HTML code
<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>
[解决办法]
if (province.options[i].value == hid1.value) {
province.value = province.options[i].value;
break;
}
[解决办法]
那事情算是有进展了。。。你可以考虑在alert所处的函数延迟调用

比如:
JScript code
setTimeout(function () {     ...........}, 100);
[解决办法]
探讨
楼上的大哥。你的和我的开始一样,要加
            if(province.options[i].value  == hid1.value)
            {
         alert(hid1.value);          province.value = province.options[i].value;
            break;
            }
才能加载出来。。。

[解决办法]
首先可以在添加option的同时设置:

if(i == 4){ op.selected = true; }
还可以用setTimeout延迟一下:

setTimeout(function(){ oSelect.selectedIndex = iIndex; }, 0);
最后是比较推荐的用setAttribute来设置:

oSelect.options[iIndex].setAttribute("selected", "true");

参考这里

以前测试的,不知对不对

热点排行