使用 javascript 附加选择选项
我目前有一个空的下拉列表,我想用 javascript 从数据库中获取的一些数据来填充它。
我真的不知道该怎么做,所以现在我正在尝试这个。
JavaScript 与 JQuery。
for (var i=0;i<data.length;i++)
{
var code$=data[i].String1;
var desc$=data[i].Code_Description;
var str$=$('<option value='+code$+'>'+desc$+'</option>');
option$.append(str$);
}
$('#offCode').html(option$);
我也尝试过
$('#offCode').append(option$);
,但这些都不起作用
谢谢
i currently have an empty drop down list that I would like to fill with some data I got from my database using javascript.
I really have no idea how to do this so right now I am trying this.
Javascript with JQuery.
for (var i=0;i<data.length;i++)
{
var code$=data[i].String1;
var desc$=data[i].Code_Description;
var str$=$('<option value='+code$+'>'+desc$+'</option>');
option$.append(str$);
}
$('#offCode').html(option$);
I have also tried
$('#offCode').append(option$);
but none of this works
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用类似的内容:
显然,上面的内容依赖于从 ajax/数据库调用中正确检索和填充的变量。
Try using something like:
The above, obviously, relies on the variables being properly retrieved and filled from your ajax/database-call.
append()
将您要创建的选项元素直接添加到您无法从代码中看出
option$
是什么片段,但append
将参数作为子元素插入。看起来您将所有选项append
'ing到一个元素,然后将该元素append
'ing到一个选择,这是行不通的。append()
the option elements you're making directly to the<select>
You can't tell what
option$
is from the code snippet, butappend
inserts the argument as a child element. It would appear that you'reappend
'ing all the options to an element, thenappend
'ing that element to a select, which would not work.下面的解决方案大致基于您的问题中提供的信息。由于未披露代码的完整范围,某些项目可能不适用。因此,该解决方案略有修改,但您应该能够轻松修改它以满足您的需求。
HTML:
JavaScript:
工作示例位于:http://jsfiddle.net/nAPDq/
The solution below is loosely based off of the information provided within your question. Some items may not be applicable as the full scope of your code was not disclosed. As a result, the solution is slightly modified but you should be able to easily modify it to fit your needs.
HTML:
JavaScript:
Working example at: http://jsfiddle.net/nAPDq/