【问题标题】:Uncaught ReferenceError: calculateTotal is not defined未捕获的 ReferenceError:未定义 calculateTotal
【发布时间】:2020-03-12 02:02:01
【问题描述】:

单击计算按钮后,它不显示返回值,而是显示此错误:“未捕获的引用错误:未定义计算总计”。我对 JavaScript 很陌生。请帮忙。

HTML:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>2</title>
        <meta charset="utf-8"/>
        <script type="text/javascript" src="2.js"></script>
    </head>
    <body>
        <div id="page">
        <div id="header">
            <h1> 02 </h1>
        </div>
        <div id="body">
            <p>You probably want to retire someday so let's get started!</p>
                <form name="retirement savings" action="#" OnClick="calculateTotal(amountNeeded.value, investNo, annContribute,inrate)">
                    How much money do you need to retire: <input type="text" name="savingsGoal"><br><br>
                    Initial Investment: <input type="text" name="initialInvestment"><br><br>
                    Annual Contribution: <input type="text" name="annualContribution"><br><br>
                    Expected Interest Rate: Select from the following please:<br><br>
                        <input type="radio" name="interestRate" value="1percent">1 Percent<br>
                        <input type="radio" name="interestRate" value="1.5percent">1.5 Percent<br>
                        <input type="radio" name="interestRate" value="2percent">2 Percent<br>
                        <input type="radio" name="interestRate" value="2.5percent">2.5 Percent<br>
                        <input type="radio" name="interestRate" value="3percent">3 Percent<br>
                        <input type="radio" name="interestRate" value="3.5percent">3.5 Percent<br>
                        <input type="radio" name="interestRate" value="4percent">4 Percent<br>
                        <input type="radio" name="interestRate" value="4.5percent">4.5 Percent<br>
                        <input type="radio" name="interestRate" value="5percent">5 Percent<br>
                        <input type="radio" name="interestRate" value="5.5percent">5.5 Percent<br>
                        <input type="radio" name="interestRate" value="6percent">6 Percent<br>
                        <input type="radio" name="interestRate" value="6.5percent">6.5 Percent<br><br>
                        <input type="button" onClick="calculateTotal(savingsGoal.value, initialInvestment, annualContribution,interestRate);" value="Calculate">  <input type="reset">
                </form>
                <br>
                <hr>
                <p>You can retire in years</p>
                <p>with in the bank</p>
                <hr>                
        </div>
        <div id="footer"><br>
        <a href="../index.html">Return to Main Menu</li>
        </div>
    </body>
</html>

JS:

/*Interest Radio Button*/
var interest_rate= new Array();
interest_rate["1percent"]=1;
interest_rate["1.5percent"]=1.5;
interest_rate["2percent"]=2;
interest_rate["2.5percent"]=2.5;
interest_rate["3percent"]=3;
interest_rate["3.5percent"]=3.5;
interest_rate["4percent"]=4;
interest_rate["4.5percent"]=4.5;
interest_rate["5percent"]=5;
interest_rate["5.5percent"]=5.5;
interest_rate["6percent"]=6;
interest_rate["6.5percent"]=6.5;


function getIntrestRate() {
    var interestRadio = document.getElementsByName('interestRate');

    for (i=0; i < interestRadio.length; i++) {
        if (interestRadio[i].checked) {
            user_input = interestRadio[i].value;
        }
    }

    return interest_rate[user_input];
}

/*Calculation*/

function calculateTotal(savingsGoal.value, initialInvestment.value, annualContribution.value, interstRate.value)(){
 if ((document.calc.savingsGoal.value == null || document.calc.savingsGoal.length == 0) ||
     (document.calc.initialInvestment.value == null || document.calc.initialInvestment.length == 0)||
     (document.calc.annualContribution.value == null || document.calc.rate.annualContribution.length == 0)){
     alert("Please fill in required fields");
     return false;
}

 else
 {
 var amount = document.calc.savingsGoal.value;
 var invest  = document.calc.initialInvestment.value;
 var yearly   = document.calc.annualContribution.value / 1200;
 var interest_rate= document.calc.getInterestRate.value;
 document.calc.pay.value = amount * interest_rate / (1 - (1/(1 + intr), yearly)));
 }
}

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    你的函数有 invalid definition() 内的参数后有额外的()

    function calculateTotal(savingsGoal.value, initialInvestment.value,
    annualContribution.value, interstRate.value)(){
                                              //^^remove this
    

    更新

    我刚刚指出的几点在您的情况下仍然无效。因此,只需浏览此 fiddle 中显示的代码。我已经更新了htmljs。我已将获取值的逻辑更改为function.. 现在它的参数少了functiongetIntrestRate() 也是一个函数,您试图将其作为变量/元素访问,而您调用它的方式是错误的。有一个拼写错误,即getInterestRate。只需通过小提琴,请注意它不完全是functional。不知道你最后在intr 变量中得到了什么。

    【讨论】:

    • 是的,我同意这一点。您也可以从函数定义中删除参数,因为您不在函数定义中使用它们。使用类似函数calculateTotal(){
    • 我删除了那个。但仍然得到那个错误。还有这个错误:“Uncaught SyntaxError: Unexpected token .”
    【解决方案2】:

    calculateTotal 函数未正确定义,因为其签名中存在语法错误。

    做起来

    function calculateTotal(savingsGoal, initialInvestment, annualContribution, interstRate){
    

    在定义方法时,您不能在此处访问对象的属性。

    还有一个额外的括号()

    【讨论】:

      【解决方案3】:

      我认为您不包含 Jquery 库文件,因此您在其工作的标题上包含 jquery 库文件

      https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js
      

      或搜索最新版本的jquery库文件包含。

      【讨论】:

        猜你喜欢
        • 2023-01-23
        • 2016-11-03
        • 2011-01-05
        • 2016-01-02
        • 2013-10-06
        • 2016-12-17
        • 1970-01-01
        相关资源
        最近更新 更多