【发布时间】:2023-01-15 10:25:32
【问题描述】:
JavaScript 新手在这里。 我的任务是使用给定的方程式计算用户的每月付款。我无法从用户那里获取值。
const loanAmount = document.getElementById('loan-amount');
const loanYears = document.getElementById('loan-years');
const loanRate = document.getElementById('loan-Rate');
const span = document.querySelector('span');
const form = document.getElementById("calc-form");
form.addEventListener("submit", function (e) {
e.preventDefault();
console.log('hello')
makeLogo();
});
function makeLogo(loanAmount, loanYears, loanRate) {
const principle = loanAmount.value
const n = loanYears.value * 12;
const i = loanRate.value / 12;
const monthylPayment = (principle* i)/1-(1+ i)** -(n);
span.innerText = monthylPayment;
}
这是我到目前为止所拥有的,并且在 makeLogo 函数中的变量出现错误。
【问题讨论】:
-
我建议您首先解析输入的值,然后将这些值用作 makeLogo() 的参数,然后返回值并分配给您的 span.innerText 这与当前让您的函数处理元素本身不同。它应该使调试更容易。
-
脚本在正文底部
-
错误消息 = appTwo.js:19 Uncaught TypeError: 无法在 HTMLFormElement 的 makeLogo (appTwo.js:19:34) 读取未定义的属性(读取“值”)。<anonymous> (appTwo.js:12:5) makeLogo @ appTwo.js:19(匿名)@appTwo.js:12
-
function makeLogo(loanAmount, loanYears, loanRate)这意味着该函数需要 3 个参数,并且由于您在调用makeLogo();时没有传递任何参数,因此它们都是未定义的。要让函数使用全局变量,请删除参数:function makeLogo()。
标签: javascript math user-input