如何在选择元素中将一项作为选择项 [英] How to have one item as selected item in select element

查看:81
本文介绍了如何在选择元素中将一项作为选择项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我的.aspx页面中有一个select元素:

Hi I have a select element in my .aspx page:

<select id="t"></select>


我想使用Jquery动态创建选项元素:


I want to create option elements dynamically using Jquery:

$("<option>bb</option>").attr("value", "1").attr("selected", "selected").appendTo('#t');


我希望在选择元素加载时将一个项目作为选择的项目,但是当我使用上面的代码时,我看不到此结果,在IE中,没有任何项目将显示为选择的元素,而在Fire Fox中,我始终可以看到第一个元素.
有什么问题我该怎么办?
请帮帮我.
在此先感谢


I want to have one item as selected item when my select element load,but when i use code above I can`t see this result,in IE no item will show as selected element and in Fire Fox I can always see the first element.
What is the problem?What can I do?
Please help me.
Thanks in advance

推荐答案

(" ).attr( value"" ).attr(" 已选择" 已选择").appendTo(' #t');
("<option>bb</option>").attr("value", "1").attr("selected", "selected").appendTo('#t');


我希望在选择元素加载时将一个项目作为选择的项目,但是当我使用上面的代码时,我看不到此结果,在IE中,没有任何项目将显示为选择的元素,而在Fire Fox中,我始终可以看到第一个元素.
有什么问题我该怎么办?
请帮帮我.
在此先感谢


I want to have one item as selected item when my select element load,but when i use code above I can`t see this result,in IE no item will show as selected element and in Fire Fox I can always see the first element.
What is the problem?What can I do?
Please help me.
Thanks in advance


通过"true"代替值,

Pass ''true'' in place of value,

.attr("selected", true)



另外,我建议使用最新的.prop()而不是.attr()



Also, I suggest using the latest .prop() instead of .attr()

.prop("selected", true)



希望对您有所帮助.



Hope it helps.


感谢您的回答.
我使用以下方法:
Thanks for your answer.
I use these methods:


这篇关于如何在选择元素中将一项作为选择项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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