您好我尝试在for循环的下拉菜单中添加值,如图here所示 以下是供您参考的代码:
<div class="well carousel-search hidden-phone">
<div class="btn-group">
<a class="btn dropdown-toggle btn-select" data-toggle="dropdown" href="#">Choose Month 1 <span class="caret"></span></a>
<ul id = "dd1" class="dropdown-menu"></ul>
</div>
<div class="btn-group">
<a class="btn dropdown-toggle btn-select2" data-toggle="dropdown" href="#">Choose Month 2 <span class="caret"></span></a>
<ul id = "dd2" class="dropdown-menu"></ul>
</div>
</div>
,Javascript如下:
$(document).ready(function(){
var d=new Date();
var year = ["2013","2014"];
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
for(i=0; i<monthNames.length; i++)
$("#dd1").append("<li><a href=''>"+monthNames[i]+"</a></li>");
$("#dd2").append("<li><a href=''>"+monthNames[i]+"</a></li>");
});
其中一个下拉菜单列表显示单个值'undefined'。知道为什么吗?
答案 0 :(得分:2)
你的for循环需要{}才能执行$(“#dd2”)行
for(i=0; i<monthNames.length; i++)
{
$("#dd1").append("<li><a href=''>"+monthNames[i]+"</a></li>");
$("#dd2").append("<li><a href=''>"+monthNames[i]+"</a></li>");
}