我想创建一个包含递减顺序日期的下拉列表。例如,从当前月份开始,格式为“6 2012”,一直到“2011年3月”。
我有一个jsfiddle,这是代码:
var TheStartDate = new Date(2011, 4, 2);
var TheCurrentDate = new Date();
TheCurrentDate = new Date(TheCurrentDate.getUTCFullYear(),
TheCurrentDate.getMonth(), 2);
var TheHTML = '<select>';
do {
TheHTML = TheHTML + '<option>';
TheHTML = TheHTML + (TheCurrentDate.getUTCMonth() + 1).toString() + " ";
TheHTML = TheHTML + TheCurrentDate.getFullYear();
TheHTML = TheHTML + '</option>';
TheCurrentDate.setMonth(TheCurrentDate.getUTCMonth() - 1);
} while (TheStartDate.getTime() !== TheCurrentDate.getTime());
TheHTML = TheHTML + '</select>';
alert(TheHTML);
document.getElementById('TheTestDiv').innerHTML = TheHTML;
我不是太远,但我认为我错过了一些明显的东西。
感谢您的建议。
答案 0 :(得分:1)
试试这个:
var toDate = new Date(2011, 2, 1);
var theDate = new Date();
var jDateSelect = jQuery('<select />');
do {
var jDateOption = jQuery('<option />');
jDateOption.text((theDate.getUTCMonth() + 1).toString() + " " + theDate.getUTCFullYear());
theDate.setMonth(theDate.getMonth() - 1);
//add the jQuery
jDateSelect.append(jDateOption);
} while (theDate.getTime() >= toDate.getTime());
$('#TheTestDiv')
//clear
.html('')
//append object
.append(jDateSelect);