javascript for循环减少

时间:2013-02-06 19:19:12

标签: javascript for-loop operators

我有这个jQuery for loop

for (; year <= endYear; year++) {
    html += '<option value="' + year + '"' + 
        (year == drawYear ? ' selected="selected"' : '') +
        '>' + year + '</option>';
}

year = 1950 and endYear = 2006。

我的选项值如此显示

1950
1951
1952
and so on

我试图让它反转

2006
2005
2004
and so on

如何撤销此声明for (; year <= endYear; year++) {

3 个答案:

答案 0 :(得分:8)

反转你的for循环,使其向相反的方向前进!

var startYear = 1950;
var endYear = 2006;

for (var year = endYear; year >= startYear; year--) {
     html += '<option value="' + year + '"' + (year == drawYear ? ' selected="selected"' : "") + ">" + year + "</option>"
}

答案 1 :(得分:2)

for(var year = endYear; year >= startYear; year=year-1) {
}

答案 2 :(得分:0)

这解决了我的问题

for (year=endYear; year >= 1950 ; year--) { 

Thanos destroy