js让怎么让月份加1

飞舞的锄头 2014-03-24 05:42:37
下面是一个用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以此类推,该怎么修改上面的代码?
...全文
754 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ali_mushishi 2014-03-25
  • 打赏
  • 举报
回复
引用 7 楼 yanocean 的回复:
[quote=引用 6 楼 yanocean 的回复:] [quote=引用 4 楼 microlab2009 的回复:] [quote=引用 2 楼 yanocean 的回复:] 你这好像反了 楼主是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] 为什么喜欢+-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);[/quote] 错了 应该是
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);
[/quote] 鹅鹅鹅 脑子不清楚。。 。 你是对的= = sorry
ali_mushishi 2014-03-25
  • 打赏
  • 举报
回复
引用 6 楼 yanocean 的回复:
[quote=引用 4 楼 microlab2009 的回复:] [quote=引用 2 楼 yanocean 的回复:] 你这好像反了 楼主是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] 为什么喜欢+-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);[/quote] 错了 应该是
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);
ali_mushishi 2014-03-25
  • 打赏
  • 举报
回复
引用 4 楼 microlab2009 的回复:
[quote=引用 2 楼 yanocean 的回复:] 你这好像反了 楼主是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] 为什么喜欢+-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);
阿鱼 2014-03-25
  • 打赏
  • 举报
回复
....... var datestr = year+"-"+month; var valueMonth = (month===12)? '01':'0'+(parseInt(month)+1), valueStr = (month===12)? (year+1+'-'+valueMonth):(year+'-'+valueMonth); document.write("<option value="+valueStr+'-01'+">"+datestr+"</option>"); .......
嘻哈大咖秀 2014-03-24
  • 打赏
  • 举报
回复
引用 2 楼 yanocean 的回复:
你这好像反了 楼主是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);
ali_mushishi 2014-03-24
  • 打赏
  • 举报
回复
引用 1 楼 microlab2009 的回复:
document.write("<select id='et'>");
			var year =2008;
			var month =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;

				}

			 var datestr = year+"-"+month;
			 var split = datestr.split('-');
			 var value = (parseInt(split[1]) - 1) < 1 ? (parseInt(split[0]) - 1) + "-12" : (parseInt(split[1]) - 1) < 10 ? split[0] + "-0"+(parseInt(split[1]) - 1) : split[0] + "-"+(parseInt(split[1]) - 1);
			 document.write("<option value="+value+">"+datestr+"</option>");
			 month++;
			}
			document.write("</select>");

			document.getElementById('et').options[73].selected="selected";
你这好像反了 楼主是value的值比 文本的大一个月
嘻哈大咖秀 2014-03-24
  • 打赏
  • 举报
回复
document.write("<select id='et'>");
			var year =2008;
			var month =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;

				}

			 var datestr = year+"-"+month;
			 var split = datestr.split('-');
			 var value = (parseInt(split[1]) - 1) < 1 ? (parseInt(split[0]) - 1) + "-12" : (parseInt(split[1]) - 1) < 10 ? split[0] + "-0"+(parseInt(split[1]) - 1) : split[0] + "-"+(parseInt(split[1]) - 1);
			 document.write("<option value="+value+">"+datestr+"</option>");
			 month++;
			}
			document.write("</select>");

			document.getElementById('et').options[73].selected="selected";

87,922

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧