JavaScript 代码不打印消息

标签 javascript html

所以突然间我的代码在执行时不再打印任何内容,HTML 似乎正在工作,但是当单击计算按钮时什么也没有发生,(程序还远未完成,但需要首先解决计算按钮的问题) 。尝试找出语法错误和小错误,但我猜还剩下一些,因为它在添加 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>

最佳答案

有两件事

你有

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

应该是这样的:

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

您的代码中还需要结束 }

这里正在工作:http://codepen.io/nilestanner/pen/WGAdoB

关于JavaScript 代码不打印消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39471292/

相关文章:

javascript - React 子组件调用兄弟组件上的函数

html - 字体弄乱了数字的对齐方式

html - 如何对多行复杂记录进行 HTML 布局?

javascript - Dojo 1.7 Ajax 内容和 AMD 要求

javascript - 使用 jQuery 在更改时显示范围字段值

python - 如何选择美丽汤列表中每个元素的第一个子元素

html - 更改下载的模板导航颜色困惑

javascript - window.open 无法按预期工作,需要一些建议

javascript - 如何通过单击共享点中的按钮关闭 SP.UI.ModalDialog?

c# - 如何在一页中使用两个表单?