call函数无法工作

call Function failing to work

本文关键字:工作 函数 call      更新时间:2023-09-26

我正在编写一个简单的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;
    }
}

您的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;
}

find_amount() {...}之前"缺少"onSelectChange(combo) {...函数的右括号。您在函数结束后关闭了它。

因此函数find_amount被定义/嵌套在onSelectChange(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;
}