JS计算协助

JS Calculation assistance

本文关键字:计算 JS      更新时间:2023-09-26

我的代码有一些问题,我正在创建一个网站,让它计算首付款为的抵押贷款:前25000美元的3%为需要首付的房屋抵押贷款投保,具体如下:前25000美元的3%剩余的5%

输入包括SSN和抵押贷款金额。我希望它打印申请人的SSN和所需的首付款金额。拒绝任何超过70000美元的申请。不要忘记验证您的输入。如果输入不好,我希望它显示一条错误消息,并再次要求输入数据。

<html>
<head>
<title>Mortgage Charges</title>
<script type="text/javascript"> 
// Program name: FHA 
// Purpose: print the applicant’s SSN and the amount of down payment required
// Date last modified: 3/29/12
function mortgage() {
    var amtOwed = parseInt(document.frmOne.ssn.value);
    var mortgage = 0;
    if (mortgage <= 25000) {
        amtOwed = 0;
    }
    else if (mortgage >= 5%) {
    }
    alert(amtOwed);
    document.frmOne.mortage.value = amtOwed;
}
window.onload = function() {
    document.frmOne.onsubmit = function(e) {
       mortgage();
       return false;
    };
};
</script>
</head>
<body>
<form name="frmOne">
Enter your SSN:<input type="text" id="ssn" /><br />
Mortgage amount:<input type="text" id="mortage" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html> 

恐怕我无法理解你的逻辑。你认为这是怎么回事?

让我分解一下你当前的代码:

function mortgage() {
    var amtOwed = parseInt(document.frmOne.ssn.value);
    // Get the value from the text box, and convert it to a number. That's good.
    var mortgage = 0;
    // Initialise a variable. Fair enough.
    if (mortgage <= 25000) {
    // You JUST set morgage=0. How can it be anything but less than 25k?
        amtOwed = 0;
        // You are overwriting the value you got from the form with 0
    }
    else if (mortgage >= 5%) {
        // Okay, first of all this else will never be reached, see comment above.
        // Second... 5% of what, exactly? If you want 5% of a number, multiply the number by 0.05
        // Third, what's the point of this block if there's no code in it?
    }
    alert(amtOwed);
    document.frmOne.mortage.value = amtOwed;
}

基本上,您的代码可以简化为:

function morgage() {document.frmOne.mortage.value = 0;}

因为这就是它的全部作用。

我真的不明白你在做什么,但希望解释一下你目前的尝试能帮助你找到答案。