js---switch问题--求解答

js—switch问题–求解答

需要完成程序功能 -----编写函数:用switch循环来实现当输入月份时,输出月天数;

界面

在这里插入图片描述

出现bug

输入月份之后 点击动不了 怀疑是switch中 value2 出现问题

将 value2.value之后运行成功

不知道原因 求解答

出现问题代码

!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>123456789</title>
		<style type="text/css">

		</style>
	</head>
	<body>
		
		<input type="text"  id="text1" />
		<button id="btn">点我</button>
		<input type="text" id="text2" />
		<script type="text/javascript">
			
			btn.addEventListener("click",function(){dian()});
		function dian(){
			var value1=document.getElementById("text1").value;
			var btn=document.getElementById("btn");
			var value2=document.getElementById("text2").value;   //感觉问题所在
			console.log(value2);
			switch(value1){
				case '1' : value2="31";break;
				case '3' : value2="31";break;
				case '5' : value2="31";break;
				case '8' : value2="31";break;
				case '1' : value2="31";break;
				case '12': value2="31";break;
				case '2' : value2="28";break;
				case '4' : value2="30";break;
				case '6' : value2="30";break;
				case '7' : value2="30";break;
				case '9' : value2="30";break;
				case '11' : value2="30";break;
				default : value2="输入数字";break;
			}
		}
			
			
		</script>
	</body>

</html>




 ***改过代码 ---成功*** 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>123456789</title>
		<style type="text/css">

		</style>
	</head>
	<body>
		
		<input type="text"  id="text1" />
		<button id="btn">点我</button>
		<input type="text" id="text2" />
		<script type="text/javascript">
			
			btn.addEventListener("click",function(){dian()});
		function dian(){
			var value1=document.getElementById("text1").value;
			var btn=document.getElementById("btn");
			var value2=document.getElementById("text2");   //修改过
			console.log(value2);
			switch(value1){
				case '1' : value2.value="31";break;              //都改成 value2.value
				case '3' : value2.value="31";break;
				case '5' : value2.value="31";break;
				case '8' : value2.value="31";break;
				case '1' : value2.value="31";break;
				case '12': value2.value="31";break;
				case '2' : value2.value="28";break;
				case '4' : value2.value="30";break;
				case '6' : value2.value="30";break;
				case '7' : value2.value="30";break;
				case '9' : value2.value="30";break;
				case '11' : value2.value="30";break;
				default : value2.value="输入数字";break;
			}
		}

		</script>
	</body>

</html>




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值