博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jquery IE6 select.val() bug报错解决办法
阅读量:4353 次
发布时间:2019-06-07

本文共 1052 字,大约阅读时间需要 3 分钟。

原文地址: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

你可能感兴趣的文章
Arraylist集合遍历输出
查看>>
java中的选择结构与循环结构
查看>>
无法将类型“ASP.login_aspx”转换为“System.Web.UI.WebControls.Login”
查看>>
[cocos2dx] lua注册回调到c++
查看>>
(treap)[bzoj3224][洛谷3369][cogs1829]Tyvj 1728 普通平衡树
查看>>
Linux下常用的shell命令记录
查看>>
HTTP 常用 Header 讲解
查看>>
linux分割字符串操作
查看>>
PHP学习2
查看>>
多实例Mysql配置
查看>>
linux下安装Mongodb
查看>>
Page.RegisterStartupScript和Response.Write的区别。
查看>>
hdu4348区间更新的主席树+标记永久化
查看>>
ZOJ 2532 Internship
查看>>
HDU 3452 Bonsai
查看>>
[Erlang12] Mnesia分布式应用
查看>>
图的遍历 | 1013 连通块块数
查看>>
Kinect 开发 —— 进阶指引(上)
查看>>
python学习笔记(六)time、datetime、hashlib模块
查看>>
uva489(需要考虑周全)
查看>>