JavaScript代码不会打印出消息

时间:2016-09-13 13:26:04

标签: javascript html

所以突然间,我的代码在执行时不再打印任何内容,HTML似乎正在工作,但是当点击计算按钮时没有任何反应(程序远未完成,但需要首先使用计算按钮解决问题) 。尝试过fins语法错误和小错误,但我猜测还有一些,因为它在添加if语句之前首先工作。

<html>
<head>

    <meta name="group" content="95">
    <meta name="author" content="Carolina Svantesson">
    <meta name="author" content="Viktoria Hamnér"> 
    <title>Calendar</title>     

</head>

<body>
    <h1>Weekday Calculator</h1> 

    <form id="inputForm">
        <table>
        <tr>
            <td>Year:</td>
            <td><input type ="text" id="year" value="" size="4"></td>    
        </tr>

        <tr>
            <td>Month:</td>
            <td><input type ="text" id="month" value="" size="2"></td>
        </tr>

        <tr>
            <td>Day:</td>
            <td><input type ="text" id="day" value="" size="2"></td>
        </tr>

            <td>&nbsp;</td>
            <td><input type ="button" id="button" value="Calculate" 
                onclick="handleInput(this.form);"></td>
              </tr>
        </table>
    </form>

    <p id="output"></p>

    <script language="Javascript">
    function handleInput(form){
       // var form = document.getElementById("inputForm");
        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 the expected 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 expected 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 expected range (1--31).";

            if (intMonth == 4, 6, 9, 11)
                if (intDay <1 || intDay>30)
                    throw "Incorrect input, Day is out of expected range(1--30)";

            if (intMonth == 2)
                if (intYear % 4== 0 && %100 !==0)
                    if (intDay <1|| intDay>29)
                        throw "Incorrect input, Day is out of expected range(1--29)";
            if (intMonth == 2)
                if (intYear %4 !==0)
                    if (intDay <1|| intDay >28)
                        throw "Incorrect input, Day is out of expected range (                                  1--28)";

            var output =" "+ intYear + " " + intMonth + " " + intDay +"är en...";
            document.getElementById("output").innerHTML = output;

        }
        catch (error){
            document.getElementById("output").innerHTML="ERROR: " +error;
        }



    </script>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

两件事

你有

if (intYear % 4== 0 && %100 !==0)

应该是这样的:

if (intYear % 4== 0 && intYear % 100 !==0)

您还需要在代码中关闭}

这里有效:http://codepen.io/nilestanner/pen/WGAdoB