HTML / Javascript日历.日期范围不起作用
HTML/Javascript calender. Range of dates doesn´t work
好吧,所以我想做这个简单的工作日计算器,我不能得到的日期范围工作。例如,如果我输入2012-05-31,它会显示超出范围(1-30),但由于5月份有31天,所以它是不正确的。有人能解释一下为什么它不起作用吗?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Weekday calculator</title>
<style type="text/css">
body {background-color: maroon; color:white; font-family: helvetica; text-align: center;}
h1 {font-size: 50px;}
table { margin-left: auto; margin-right: auto;
text-align:right; background-color:#993300 ; border: 5px solid
firebrick }
</style>
</head>
<body>
<h1>Weekday calculator</h1>
<br>
</p>
<br><br>
<form id="dateForm">
<table>
<tr>
<td>Year</td>
<td><input type="text" id="year" value="" size="8"></td>
</tr>
<tr>
<td>Month</td>
<td><input type="text" id="month" value="" size="8"></td>
</tr>
<tr>
<td>Day</td>
<td><input type="text" id="day" value="" size="8"></td>
</tr>
<tr>
<td> </td>
<td><input type="button" id="button" value="Submit" onclick="handleInput(this.form);"></td>
</tr>
</table>
</form>
<p id="output"></p>
<script language="Javascript">
function handleInput(form) {
try {
var strYear = form.year.value;
var strMonth = form.month.value;
var strDay = form.day.value;
var intYear = parseInt(strYear);
var intMonth = parseInt(strMonth);
var intDay = parseInt(strDay);
if (isNaN(intYear))
throw "Incorrect input. Year is not a number.";
if (intYear < 0 || intYear > 9999)
throw "Incorrect input. Year is out of range (0--9999).";
if (isNaN(intMonth))
throw "Incorrect input. Month is not a number";
if (intMonth < 1 || intMonth > 12)
throw "Incorrect input. Month is out of range (1--12).";
if (isNaN(intDay))
throw "Incorrect input. Day is not a number.";
if (intMonth == 1, 3, 5, 7, 8, 10, 12)
if (intDay < 1 || intDay > 31)
throw "Incorrect input. Day is out of range (1--31).";
if (intMonth == 4, 6, 9, 11)
if (intDay < 1 || intDay > 30)
throw "Incorrect input. Day is out of range (1--30).";
if (intMonth == 2)
if (intYear % 4 == 0 && intYear % 100 != 0)
if (intDay < 1 || intDay > 29)
throw "Incorrect input. Day is out of range (1--29).";
var output = "It´s a... " ;
document.getElementById("output").innerHTML = output;
}
catch (error) {
document.getElementById("output").innerHTML = "ERROR: " + error;
}
}
</script>
</body>
</html>
这就是问题所在:
if (intMonth == 1, 3, 5, 7, 8, 10, 12)
JavaScript(和大多数语法与JavaScript相似的语言)没有将值与值列表进行比较的语法。(这不是语法错误,因为JavaScript,有点不典型,有逗号操作符, if
最终成为if (12)
。)
用||
if (intMonth == 1 || intMonth == 3 || ...)
或者使用switch
switch (intMonth) {
case 1:
case 3:
case 5:
// ...
// Code for these months here
break;
case 2:
// Code for Feb here
break;
// ...
}
或者使用查找对象:
var maxDays = {
1: 31,
2: null, // Handle leap years separately
3: 31,
4: 30,
// ...and so on
};
var maxDay;
if (maxDay == 2) {
maxDay = /* handle leap years*/;
} else {
maxDay = maxDays[intMonth];
}
相关文章:
- 输入类型范围在angularjs重复内不起作用
- 对于使用传递的数据计算的局部范围变量,角度绑定在自定义指令中不起作用
- Angularjs 更新 setTimeout 中的范围变量不起作用
- 如果端口范围太宽,节点中的端口扫描不起作用
- Angular:从工厂更新范围不起作用
- 范围的Switch语句不起作用
- AngularJS-ng显示范围变量不起作用
- 角度范围不起作用
- Javascript 全局范围不起作用
- 样式<输入类型=“范围”>用于重定向,在触摸设备上不起作用
- jQuery 自定义事件在全局范围内工作,但在对象之间不起作用
- 高于 1 的嘶嘶声范围不起作用
- 通过指令范围传递对象不起作用
- 创建范围功能在除IE以外的其他浏览器中不起作用
- Javascript范围在角度SPA应用程序中不起作用
- 角度范围.$watch在指令中不起作用
- 茉莉花+伊斯坦布尔覆盖范围不起作用
- 监视指令范围不起作用
- HTML / Javascript日历.日期范围不起作用
- 克隆范围不起作用