Jquery数组值加载问题

时间:2015-07-23 18:44:52

标签: jquery

我创建了一个数组并将其加载到html页面,但它只加载了我的数组的部分内容(2015年9月29日停止)。我不知道为什么它不加载所有数据?以下是我的代码:     clsstartdate.js代码:

(function(){
    window.onload= function addList(){   
     var currentDt = new Date();      
     var mm = currentDt.getMonth() + 1;
    //if(mm <= 9) mm = '0'+mm;
    var dd = currentDt.getDate();
    //if(dd<= 9) dd = '0'+dd;
    var yyyy = currentDt.getFullYear();
    var date = mm + '/' + dd + '/' + yyyy;

    var select = document.getElementById("zstartdate"); 
var options = ["8/24/2015","8/25/2015","8/27/2015","8/28/2015","8/29/2015","8/31/2015","9/1/2015","9/2/2015","9/3/2015","9/5/2015","9/7/2015","9/8/2015","9/9/2015","9/10/2015","9/12/2015","9/13/2015","9/14/2015","9/15/2015","9/16/2015","9/17/2015","9/18/2015","9/19/2015","9/21/2015","9/23/2015","9/24/2015","9/25/2015","9/26/2015","9/28/2015","9/29/2015","10/2/2015","10/5/2015","10/16/2015","10/17/2015","10/18/2015","10/19/2015","10/21/2015","10/22/2015","10/23/2015","10/26/2015","10/27/2015","10/28/2015","11/7/2015","11/11/2015","11/13/2015","11/14/2015","11/16/2015","11/18/2015","11/19/2015","11/21/2015","11/23/2015","11/29/2015"]; 

        for(var i = 0; i < options.length; i++) 
        {
            var opt = options[i];

            if (opt > date)
            {
             var option = document.createElement('option');
             option.text = option.value =opt;

            select.add(option, 0);
            }

        }
    }
    })();

下面是html页面代码:

<html>
<head>       
<script src="clsstartdate.js" type="text/javascript"></script>       
</head>
<body>     
<form action="schedule.html" id="form" method="get" name="form">
Start Date:
<select id="zstartdate" name="zstartdate">
<option value="-1">Select</option>
</select>
<input name="submit" type="submit" value="Search"/></form>     
</body>
</html>

1 个答案:

答案 0 :(得分:0)

当您真的想要使用日期比较时,您正在使用字符串比较。

您可以直接使用比较运算符比较两个日期对象,这样您就可以从cuurentDt中删除日期解析为字符串并执行以下操作

    var currentDt = new Date();

    for (var i = 0; i < options.length; i++) {
      var opt = options[i];
      // create date object from array value and compare to  currentDt
      if (new Date(opt) > currentDt) {
        var option = document.createElement('option');
        option.text = option.value = opt;

        select.add(option, 0);
      }

    }

DEMO