简体   繁体   English

我似乎看不到我的 JavaScript 和 HTML 代码中缺少什么

[英]I can't seem to see what's missing in my JavaScript and HTML code

I'm new to JavaScript and am having a difficult time having my computations displayed.我是 JavaScript 的新手,并且很难显示我的计算结果。 The message on my JavaScript code said that it was missing a declaration variable.我的 JavaScript 代码上的消息说它缺少声明变量。 I really don't know what else to try since I'm new to this and I've been stuck on this for a few days now, it's extremely embarrassing.我真的不知道还能尝试什么,因为我是新手,而且我已经坚持了几天了,这非常尴尬。 Here is my HTML code where the computations should display and the corresponding JavaScript to it:这是我的 HTML 代码,计算应显示在该代码中,以及相应的 JavaScript 代码:

function compute() {
    var principal = parseFloat(document.getElementById("principal").value);
    var rate = parseFloat(document.getElementByIdementById("rate").value);
    var years = parseFloat(document.getElementById("years").value);
    var result = principal * years * rate /100;
    var years = new Date().getFullYear()+parseInt(years);
}

<button onclick="compute()">Compute Interest</button>
<span id="result"></span>
<p id="result"></p>

<script>
function compute() {
   document.getElementById("result").innerHTML = "If you deposit<mark>"
   +principal+"</mark>,<br/> at an interest rate of<mark>"
   +rate+"%</mark><br/>. You will receive an amount of<mark>"+result+
   ",</mark><br/>in the year<mark>"+year+"</mark><br/>";
}
</script>

Welcome on StackOverflow.欢迎使用 StackOverflow。

Let's go to watch your code, there are several errors:让我们 go 看你的代码,有几个错误:

  1. You have declared a function compute() (the first one) that create and assign a value to some variables whose scope is inside the function!您已经声明了一个 function compute() (第一个),它创建并为 scope 在函数内部的一些变量赋值! In the second compute() function you are trying to access the value of those variables that are not declared inside it and whose scope is limited to the first function -> so the compiler will give the error Undefined variable .在第二个compute() function 中,您试图访问那些未在其中声明的变量的值,并且其 scope 仅限于第一个Undefined variable -> 因此编译器将给出错误。

  2. There is a misspell of getElementById : var rate = parseFloat(document.getElementByIdementById("rate").value); getElementById有一个拼写错误: var rate = parseFloat(document.getElementByIdementById("rate").value);

  3. In the first compute() function you have declared a variable using the same name years -> better to use a different name or simply overwrite it.在第一个compute() function 中,您已经声明了一个使用相同名称years的变量 -> 最好使用不同的名称或简单地覆盖它。

  4. You have used the same id="result" in two different HTML tags, that does not have sense because id should identify only one element;您在两个不同的 HTML 标记中使用了相同的id="result" ,这没有意义,因为id应该只标识一个元素; I suppose that you want to put the result of the elaboration inside the <p> element.我想您想将详细说明的结果放在<p>元素中。

So you have to do modify your code and put it inside one compute() function:因此,您必须修改您的代码并将其放入一个compute() function 中:

<script>
function compute() {
    var principal = parseFloat(document.getElementById("principal").value);
    var rate = parseFloat(document.getElementById("rate").value);
    var years = parseFloat(document.getElementById("years").value);
    var result = principal * years * rate /100;
    var years_modified = new Date().getFullYear() + parseInt(years);

    document.getElementById("result").innerHTML = "If you deposit<mark>"
   + principal + "</mark> , <br/> at an interest rate of<mark>"
   + rate + "%</mark><br/>. You will receive an amount of<mark>" + result +
   ",</mark><br/>in the year<mark>" + years_modified + "</mark><br/>";
}
</script>

<button onclick="compute()">Compute Interest</button>
<span></span>
<p id="result"></p>

PS = I would like to give you a suggestion, avoid using the same name for more than one function, you may run into unexpected behavior PS = 我想给你一个建议,避免使用多个 function 的同名,你可能会遇到意想不到的行为

This should work for you:这应该适合你:

HTML: HTML:

<script>
function compute() 
  {
   var principal = parseFloat(document.getElementById("principal").value);
   var rate = parseFloat(document.getElementById("rate").value);
   var years = parseFloat(document.getElementById("years").value);
   var result = principal * years * rate /100;
   var years_result = new Date().getFullYear()+parseInt(years);
   document.getElementById("result").innerHTML = "If you deposit<mark>"
   +principal+"</mark>,<br/> at an interest rate of<mark>"
   +rate+"%</mark><br/>. You will receive an amount of<mark>"+result+
   ",</mark><br/>in the year<mark>"+years_result+"</mark><br/>";
   }
 </script>
<button onclick="compute()">Compute Interest</button>
<span id="result"></span>
<p id="result"></p>

Let me know how it goes!让我知道事情的后续!

Maybe this is will work.也许这会奏效。

function compute() {
  var principal = parseFloat(document.getElementById("principal").value);
  var rate = parseFloat(document.getElementById("rate").value);
  var years = parseFloat(document.getElementById("years").value);
  var result = principal * years * rate / 100;
  var years_result = new Date().getFullYear() + parseInt(years);

  document.getElementById("result").innerHTML = `
    If you deposit <mark>${principal}</mark>
    <br/> at an interest rate of
    <mark>${rate}%</mark><br/>.
    You will receive an amount of <mark>${result}</mark>
    <br/> in the year <mark>${years_result}</mark><br/>`;
}

You also had an error on var rate = parseFloat(document.getElementByIdementById("rate").value);你也有一个错误var rate = parseFloat(document.getElementByIdementById("rate").value);

And when specified on HTML elements, the id attribute value must be unique.并且当在 HTML 元素上指定时,id 属性值必须是唯一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM