2016-09-16 17 views
-2

さて、私はこの単純な平日の電卓をしようとしていますし、仕事の範囲を得ることができません。たとえば、2012-05-31に入力すれば、範囲外です(1-30)が、5月の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

「作業していません」を定義してください。 [jsFiddle](http://jsfiddle.net/)を作成して、何が起きているかを確認してください。 – giorgio

+0

@giorgio:いいえ、フィドルではありません。スタックスニペット( '<>'ツールバーボタン)をクリックすると、オンサイトで完成します。 –

+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]; 
} 
関連する問題