我无法让我的onclick按钮显示任何内容

时间:2015-07-17 00:30:19

标签: javascript onclick calculator

好的,所以我想在夏天学习一些JavaScript,以获得乐趣。我有一本很老的书,“JavaScript:The Definitive Guide”,它于2002年出版。嗯,我一字一句地复制了他们的第一个“试用”脚本(或者我认为嘿),当我点击“onclick”按钮时,我仍然无法显示任何内容...更多可能的是,我做了某种语法错误,但总是有可能这本书也可能过时了..无论如何,这就是我所拥有的:

<script language="JavaScript">
            function calculate() {

            var principal = document.loandata.principal.value;
            var interest = document.loandata.interest.value / 100 / 12;
            var payments = document.loandata.years.value * 12;


            var x = Math.pow(1 + interest, payments);
            var monthly = (principal*x*interest)/(x-1);


                document.loandata.payment.value = round(monthly);
                document.loandata.total.value = round(monthly * payments);
                document.loandata.totalinterest.value =
                    round((monthly * payments) - principal);
                }

                else {
                    document.loandata.payment.value = "";
                    document.loandata.total.value = "";
                    document.loandata.totalinterest.value = "";
                }
            }


                function round(x) {
                    return Math.round(x*100)/100;
                }
</script>
</body>
</html>

我对此完全是新的..所以任何帮助都会受到赞赏..另外,如果之前有人问过我很抱歉..我看了其他类似的问题并且无法解决..在所有可能性这是一个我忽略的简单语法错误。

2 个答案:

答案 0 :(得分:1)

有一个else语句没有开头if

if (/*your conditino here*/)
     round((monthly * payments) - principal);
}

else {
    document.loandata.payment.value = "";
    document.loandata.total.value = "";
    document.loandata.totalinterest.value = "";
}

答案 1 :(得分:1)

你在那里错过了一个if语句。

function calculate() {

    var principal = document.loandata.principal.value;
    var interest = document.loandata.interest.value / 100 / 12;
    var payments = document.loandata.years.value * 12;


    var x = Math.pow(1 + interest, payments);
    var monthly = (principal*x*interest)/(x-1);

    // You're missing an if statement here?
    if ('something' == 'something') {
        document.loandata.payment.value = round(monthly);
        document.loandata.total.value = round(monthly * payments);
        document.loandata.totalinterest.value = round((monthly * payments) - principal);
    } else {
        document.loandata.payment.value = "";
        document.loandata.total.value = "";
        document.loandata.totalinterest.value = "";
    }
}


function round(x) {
    return Math.round(x*100)/100;
}