呼叫功能无法正常工作

时间:2015-12-01 12:02:33

标签: javascript

我正在编写一个简单的javscript程序,并且在提交按钮上我调用了find_amount()函数,但是我继续得到" ReferenceError:未定义find_amount"。我做错了什么?我已经尝试了几件事情但是无法让它发挥作用,尽管我之前曾多次尝试过这种方法而且效果很好。

<form id="myForm">

    <strong>Loan: <input type="text" name="loan" id="loan" value="" size="10"></strong>&nbsp;&nbsp;
    <select id="duration" onclick="onSelectChange(this)" style="width: 85px">
        <option value=" "></option>
        <option value="three">3 years</option>
        <option value="five">5 years</option>
        <option value="six">6 years</option>
    </select> &nbsp;&nbsp;
    <strong>Interest: <input type="text" name="interest" id="interest" value="" size="10"></strong>
    <button type="button" onclick="find_amount();">Submit</button></br></br>
    <strong>Total amount to be returned: <input type="text" name="return" id="return" value="" size="10"></strong></br></br>
    <strong>Installments</strong>
    <select id="installments" onclick="onSelectChange(this)" style="width: 85px">
        <option value=" "></option>
        <option value="year">per year</option>
        <option value="month">per month</option>
        <option value="week">per week</option>
        <option value="day">per day</option>
    </select> &nbsp;&nbsp;<strong>:</strong>
    <span id="final"></span>
</form>

JavaScript的:

function onSelectChange(combo) {

    switch (combo.value) {
        case "three":
            {
                document.getElementById("interest").value = " 3% ";
                break;
            }
        case "five":
            {
                document.getElementById("interest").value = " 5% ";
                break;
            }
        case "six":
            {
                document.getElementById("interest").value = " 6% ";
                break;
            }

        case " ":
            {
                document.getElementById("interest").value = " ";
                break;
            }

    }
    function find_amount() {
        var total;
        var inter;
        var lo;

        lo = document.getElementById("loan").value;
        inter = document.getElementById("interest").value;
        total = (lo * inter) / 100;
        alert(total);
        //document.getElementById("return").value = total;
    }
}

2 个答案:

答案 0 :(得分:2)

您的find_amount()函数已在onSelectChange()函数中定义,并且该函数以外的任何人都无法看到它。尝试将两个函数分开:

<强> JS

function onSelectChange(combo) {

    switch(combo.value) {
        case "three" : 
        {   
            document.getElementById("interest").value = " 3% ";
            break;
        }
        case "five" : 
        {   
            document.getElementById("interest").value = " 5% ";
            break;
        }
        case "six" : 
        {   
            document.getElementById("interest").value = " 6% ";
            break;
        }

        case " " : 
        {   
            document.getElementById("interest").value = " ";
            break;
        }

    }
}
function find_amount(){
    var total;
    var inter;
    var lo;

    lo = document.getElementById("loan").value;
    inter = document.getElementById("interest").value;
    total = (lo * inter)/100;
    alert(total);
    //document.getElementById("return").value = total;
}

答案 1 :(得分:1)

你是&#34;失踪&#34;在onSelectChange(combo) {...之前find_amount() {...}的右括号。你在功能结束后关闭了它。

因此函数find_amountonSelectChange(combo)中定义/嵌套,这就是为什么它在onSelectChange之外的全局范围内不可见

我使用Visual Studio格式化代码并编辑到您的帖子。使用这样的编辑器有助于避免这样的错误。

这应该是什么样子:

function onSelectChange(combo) {

    switch (combo.value) {
        case "three":
            {
                document.getElementById("interest").value = " 3% ";
                break;
            }
        case "five":
            {
                document.getElementById("interest").value = " 5% ";
                break;
            }
        case "six":
            {
                document.getElementById("interest").value = " 6% ";
                break;
            }

        case " ":
            {
                document.getElementById("interest").value = " ";
                break;
            }

    }
}

function find_amount() {
    var total;
    var inter;
    var lo;

    lo = document.getElementById("loan").value;
    inter = document.getElementById("interest").value;
    total = (lo * inter) / 100;
    alert(total);
    //document.getElementById("return").value = total;
}