---
title: "jquery 在IE6中设置select控件的selected属性的时候"
author: "fuanyu"
date: "2011-02-22T10:50:46Z"
tags: ["Firefox", "jQuery", "浏览器"]
categories: ["Web前端"]
slug: "jquery-ie6-select-selected"
draft: false
lastmod: "2011-02-22T10:50:46Z"
id: 914282
url: "https://fuanyu.iteye.com/blog/914282"
---

在IE6中使用jquery给select 赋值选中某值时会出现如下错误：

![](http://hi.csdn.net/attachment/201011/12/0_1289532404Lr8I.gif)

赋值语句：

```
$("#selectID").val("xx");
```

```
$("#selectID option").attr("selected",true);
```

```
$("#selectID option[value='xx']").attr("selected",true);
```

在IE7,IE8,IE5.5及Firefox等浏览器下面没有问题

有人找到了一个解决的方法，就是设置selected属性的时候使用setTimeout函数 设定一个时间 即可，代码如下：

// 使用setTimeout之后问题解决 

setTimeout(function() {   
    $("#selectID option").attr("selected",true);

    $("#selectID option\[value='xx']").attr("selected",true);  
}, 1);

而我的解决方法是

 　setTimeout(function() {  
             $("#areaCode").val("&lt;s:property value='school.areaCode'/&gt;");  //进行selected选中  
            });