如何在选择元素中将一项作为选择项

编程入门 行业动态 更新时间:2024-10-27 02:31:23
本文介绍了如何在选择元素中将一项作为选择项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨 我的.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:

更多推荐

如何在选择元素中将一项作为选择项

本文发布于:2023-07-19 07:18:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154184.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   元素   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!