jquery IE6 select.val() bug报错解决办法

原文地址:http://hi.baidu.com/kinghmx/item/395dbac3261292dcef183b52 

最近在写一个页面,在出了ie6外的所有浏览器中都正常(ie7,8,9,  firefox, chrome), IE6下提示 “无法设置selected属性。未指明的错误”。

 

后来发现是jquery 在 ie6 下操作 select控件有BUG.

 

我程序中是这样使用的:

 

$("#genre").val(0);

 

改成:

 

setTimeout(function(){ 
    $("#genre").val(0); 
},1);

 

就可以了.

 

原因是:

 

Note that the error will only occur if you call appendChild, then ask for the select's childNodes, then set the selected property on the newly created option. If you set selected earlier, either before appendChild or after it, there's no problem. And if you omit childNodes, it works. The problem with jQuery is that its .val() function loops over childNodes looking for an option to set, and thus always triggers the bug.

 

 

 

最后可以定义个函数 set_select_val来统一设置 select控件的值

 

function set_select_val(sel, val) 

    if($.browser.msie && $.browser.version=="6.0") { 
        setTimeout(function(){ 
            sel.val(val); 
        },1); 
    }else { 
            sel.val(val); 
    } 
}

ie6不支持$("#" + provinceDom).attr("value", provinceDefvalue); 

官网bug描述:

http://bugs.jquery.com/ticket/2252

转载于:https://www.cnblogs.com/niaowo/p/3796082.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值