嗨 我的.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
推荐答案(" < option> bb</option>").attr(" value"," 1" ).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:
更多推荐
如何在选择元素中将一项作为选择项
发布评论