【问题标题】:next 7 days in drop-down list [duplicate]下拉列表中的未来 7 天 [重复]
【发布时间】:2015-10-01 15:07:03
【问题描述】:

我希望接下来的 7 天显示在下拉列表中。但是我的代码似乎不起作用。

请注意日期格式必须为yyyy-mm-dd

你能帮帮我吗?这是我的代码:

function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [year, month, day].join('-');
}
var curr = new Date;
var first = curr.getDate()
var firstday = (new Date(curr.setDate(first))).toString();
for (var i = 0; i < 7; i++) {
    var next = new Date(curr.getTime());
    next.setDate(first + i);
    options += '<option>' + formatDate((next.toString())) + '</option>';
}

$("#datemenu1").append(options);
$("#datemenu1").html("<option>PICK A DATE</option>");
<html>

<select id="datemenu1">
  
  <option>PICK A DATE</option>
  
</select>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script src="Text-1.js"></script>strong text
</html>

fiddle

【问题讨论】:

    标签: javascript html html-select


    【解决方案1】:

    以这种方式初始化options = "";

    <html>
        <body>
            <select id="datemenu1">
    
                <option>PICK A DATE</option>
    
            </select>
            <script src="http://code.jquery.com/jquery.min.js"></script>
            <script src="Text-1.js"></script>strong text
            <script>
                function formatDate(date) {
                    var d = new Date(date),
                            month = '' + (d.getMonth() + 1),
                            day = '' + d.getDate(),
                            year = d.getFullYear();
    
                    if (month.length < 2)
                        month = '0' + month;
                    if (day.length < 2)
                        day = '0' + day;
    
                    return [year, month, day].join('-');
                }
    
                var options = "";
                var curr = new Date;
                var first = curr.getDate()
                var firstday = (new Date(curr.setDate(first))).toString();
                for (var i = 0; i < 7; i++) {
                    var next = new Date(curr.getTime());
                    next.setDate(first + i);
                    options += '<option>' + formatDate((next.toString())) + '</option>';
                    $("#datemenu1").append(options);
                }
    
    
            </script>
        </body>
    </html>
    

    【讨论】:

      【解决方案2】:

      将此行添加到代码的顶部:

      var options = "";
      

      【讨论】:

        【解决方案3】:

        你需要添加

        var options = "";
        

        在for循环之上,然后删除

        $("#datemenu1").html("<option>PICK A DATE</option>");
        

        (否则选项都会被覆盖)。

        See this fiddle for a working version

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-04
          • 2021-09-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-13
          相关资源
          最近更新 更多