IE6 jQuery 操作 select的BUG

在给select初始化的时候,除了ie 6外的所有浏览器中都正常显示效果(ie7,8,9, firefox, chrome), IE6下提示“无法设置selected属性,未指明的错误”或者就是根本就没有显示。

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

$("#hour_").val(2);

改成:

setTimeout(function(){ 
    $("#hour_").val(2); 
},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); 
    } 
}

set_select_val($("#hour"),'2');

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值