如何在IE9中使用JQuery设置选择框的默认值? [英] How to set the default value of a select box using JQuery in IE9?

查看:62
本文介绍了如何在IE9中使用JQuery设置选择框的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下选择框.

<select id="selId">
     <option id='1' value='1'>1</option>
     <option id='2' value='2'>2</option>
     <option id='3' value='3'>3</option>
     <option id='4' value='4'>4</option>
     <option id='5' value='5'>5</option>
</select>

在jquery中,我正在执行以下操作以在选择框中选择值2.

In jquery I am doing the following to select the value 2 in the select box.:

...
$("select#selId").find("option#2").attr("selected", "selected");
...

相同的代码在IE8和Firefox的选择框中将值设置为2. 但是它在IE9中不起作用.

The same code sets the value of 2 in the select box in IE8 and Firefox. But its not working in IE9.

我正在使用JQuery 1.6.1版本

I am using JQuery 1.6.1 version

推荐答案

代替设置selected属性,只需使用.val("2").

Instead of setting the selected attribute, just use .val("2").

请参阅此处: jsFiddle

这篇关于如何在IE9中使用JQuery设置选择框的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆