日期:2014-05-16  浏览次数:20435 次

js让怎么让月份加1
下面是一个用js写的只有年和月的下拉框
<script>
document.write("<select id='et'>");
var year =2008;
var month =1;
var month1=1;
for(var i=0;i<90;i++){
 if(month>12){
year++;
month=1;
var str_temp=month.toString();
month="0"+str_temp;
}else if(month<10){
var str_temp=month.toString();
month="0"+str_temp;
}

//  if(month1>12){
//   year++;
//   month1=1;
//   month1=month+1;
//   var str_temp1=month1.toString();
//   month1="0"+str_temp1;
//   }else if(month1<10){
//    var str_temp1=month1.toString();
//    month1="0"+str_temp1;
//   }


 // var datestr1 = year+"-"+month1;
var datestr = year+"-"+month;
document.write("<option value="+datestr+'-01'+">"+datestr+"</option>");
month++;
}
document.write("</select>");

document.getElementById('et').options[73].selected="selected";
</script>

现在想让下拉框的value比显示的值多一个月
比如:显示2013-11的话value为2013-12, 显示2013-12则value为2014-01以此类推,该怎么修改上面的代码?
------解决方案--------------------
引用:
你这好像反了 楼主是value的值比 文本的大一个月 

那就把value改下
var value = (parseInt(split[1]) + 1) > 12 ? (parseInt(split[0]) + 1) + "-01" : (parseInt(split[1]) + 1) < 10 ? split[0] + "-0"+(parseInt(split[1]) + 1) : split[0] + "-"+(parseInt(split[1]) + 1);

------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

你这好像反了 楼主是value的值比 文本的大一个月 

那就把value改下
var value = (parseInt(split[1]) + 1) > 12 ? (parseInt(split[0]) + 1) + "-01" : (parseInt(split[1]) + 1) < 10 ? split[0] + "-0"+(parseInt(split[1]) + 1) : split[0] + "-"+(parseInt(split[1]) + 1);

为什么喜欢+-1呢   他这边不是用Date搞出来的月份 month不会比实际的小一呢
var value = (parseInt(split[1]) )> 12 ? (parseInt(split[0]) + 1) + "-01" : (parseInt(split[1]) ) < 10 ? split[0] + "-0"+(parseInt(split[1]) + 1) : split[0] + "-"+(parseInt(split[1]) + 1);

错了 应该是
var value = (parseInt(split[1]) )> 12 ? (parseInt(split[0]) + 1) + "-01" : (parseInt(split[1])+1 ) < 10 ? split[0] + "-0"+(parseInt(split[1])+1 ) : split[0] + "-"+(parseInt(split[1]) + 1);


鹅鹅鹅     脑子不清楚。。 。 你是对的= =  sorry