您的位置:首页 > Web前端 > JQuery

JQuery<Select>在IE下设置选中问题

2014-04-18 15:57 806 查看
JQuery<Select>在IE6下设置选中问题

在IE7下jquery对select设置选中值,一般采用的方式:

$("#select").attr("value",'<%=selectId%>');
$("#select").val('<%=selectId%>');
$("#select").get(0).value = '<%=selectId%>';

但是该JQuery语句IE6不支持,所以只能用循环,找到默认值的项,设置为选中状态:
1.
<script language="javascript" type="text/javascript">
$("#select").each(function(){
for(var i = 0; i < this.options.length; i++){
if(this.options[i].value == '<%=selectId%>'){
this.options[i].selected = "selected";
break;
}
}
});
</script>

2.
var count=$("#select")[0].length;

for(var i=0;i<count;i++)
{

if($("#select").get(0).options[i].value == "<%=selected%>")

{

$("#select").get(0).options[i].selected = true;

break;

}

}

如果下拉框的内容是动态加载进来的,会发现设置的选中状态无效,这时要先等待下拉框内容加载完毕再执行上面的方法,
setTimeout(function(){
$("#select").each(function(){
for(var i = 0; i < this.options.length; i++){
if(this.options[i].value == '<%=selectId%>'){
this.options[i].selected = "selected";
break;
}
}
});
</script>
},1000);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: