0

您好,我尝试在 for 循环中向下拉菜单中添加值,如下所示 是供您参考的代码:

      <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>");
  });

下拉菜单列表之一显示单个值“未定义”。知道为什么吗?

4

1 回答 1

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>");
      }
于 2013-10-29T18:42:51.890 回答