2016-09-16 87 views
-2

好的,所以我正在嘗試做這個簡單的週日計算器,而且我無法獲得日期範圍的工作。例如,如果我在2012-05-31中鍵入它表示它超出範圍(1-30),但由於五月份中有31天,因此它不正確。有人可以解釋爲什麼它不起作用嗎?HTML/Javascript日曆。日期範圍不起作用

<!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>&nbsp;</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> 
+1

請定義'not working'。並創建一個[jsFiddle](http://jsfiddle.net/),以便我們可以看到發生了什麼 – giorgio

+0

@giorgio:不,不是小提琴。 Stack Snippets('<>'工具欄按鈕),因此它在現場完成。 –

+0

當您提出問題時,文本區域右側有一個大的橙色**如何格式化**框,其中包含有用的信息。還有一個格式化輔助工具的整個工具欄。和一個** [?] **按鈕提供格式幫助。 *和*位於文本區域和「發佈您的問題」按鈕之間的預覽區域(以便您必須滾動查看按鈕才能找到該按鈕,以鼓勵您查看該按鈕),以顯示帖子在發佈時的樣子。明確你的帖子,並證明你花時間這樣做,提高你獲得良好答案的機會。 –

回答

1

這就是問題所在:

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]; 
}