创建下拉菜单
我花了一些时间试图找到一个优雅的跨浏览器解决方案,但没有成功。我正在尝试动态创建一个下拉列表。这是我的 html:
...
<span id="currencies">
</span>
...
在我的脚本中,我尝试执行以下操作:
- 创建一个选择元素
- 将 4 个选项放入其中 - 'AAA'、'BBB'、'CCC' 和 'DDD'
- 附加选择元素到 $('#currencies')
我假设代码要跨浏览器,应该使用 jQuery 编写。当我说优雅时,我的意思是可能试图避免太多标记,例如,这样的事情:
// THIS IS NOT A WORKING CODE
$('#currencies').appendSelect().addOption('AAA', 'AAA').addOption('BBB', 'BBB')...
也许有人有一些提示。谢谢。
I've spent some time trying to find an elegant and cross browser solution but was unable. I'm trying to dynamically create a drop down. Here is my html:
...
<span id="currencies">
</span>
...
In my script I'm trying to do the following:
- Create a select element
- Put, say, 4 options into it - 'AAA', 'BBB', 'CCC', and 'DDD'
- Append the select element to $('#currencies')
I am assuming that for the code to be cross browser it should be written using jQuery. When I say elegant I mean possibly trying to avoid too much markup, for example, something like this:
// THIS IS NOT A WORKING CODE
$('#currencies').appendSelect().addOption('AAA', 'AAA').addOption('BBB', 'BBB')...
Maybe somebody has some tip. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能会这样做:
I would probably do it like this:
您必须制作一个字符串
var string1='.........'
然后
$('#currencies').html(string1);
将为您完成这项工作
You must make A string
var string1='<option>AAA</option><option>BBB</option>.........</option>'
Then
$('#currencies').html(string1);
will do the Job for You