简体   繁体   English

需要重新加载页面才能使 onclick 功能在 JS 中工作

[英]Need to reload page to make onclick function work in JS

I just created 2 days ago a script with Javascript, cause I'm learning it.我刚刚在 2 天前用 Javascript 创建了一个脚本,因为我正在学习它。 The script basically calculate the missing elements on a Circle.该脚本基本上计算圆形上缺少的元素。 For example, if I have a radius of 2, the script will return the diameter of 4, the circumference of 4π or I can have it to do 4 * Math.PI(so 4 * 3.14).例如,如果我的半径为 2,脚本将返回 4 的直径,4π 的周长,或者我可以让它做 4 * Math.PI(所以 4 * 3.14)。 Now the script basically works, except when I open the page for the first time and I put a value number in one of the input, and i click my button, "calcola"(sorry, it's in italian, i just want you to look at the core of the script) nothing happens, I have to reload the page 1 time or even 2 to see the results.现在脚本基本上可以工作了,除非我第一次打开页面并在其中一个输入中输入一个数值,然后我点击了我的按钮“calcola”(对不起,它是意大利语,我只是想让你看看在脚本的核心)没有任何反应,我必须重新加载页面 1 次甚至 2 次才能看到结果。 And I don't know why.我不知道为什么。 I think it's a cache problem or cause the browser or the js file can't get the value after the first page load... I have an external script file, and I've put it at the end of the html page, before the body tag closing.我认为是缓存问题或者导致浏览器或js文件在第一页加载后无法获取值...我有一个外部脚本文件,我已经把它放在了html页面的末尾,之前body 标签关闭。 For the script part, here it is:对于脚本部分,这里是:

 window.onload = function() { var r = document.getElementById("raggio").value; var d = document.getElementById("diametro").value; var C = document.getElementById("circonferenza").value; var A = document.getElementById("area").value; var calc = document.getElementById("calc"); var rAtPow = new Number(); var rResult = document.getElementById("r"); var dResult = document.getElementById("d"); var CResult = document.getElementById("C"); var AResult = document.getElementById("A"); rResult.innerHTML = "Raggio:"; dResult.innerHTML = "Diametro:"; CResult.innerHTML = "Circonferenza:"; AResult.innerHTML = "Area:"; calc.onclick = function() { if (r.length > 0) { rAtPow = Math.pow(r, 2); d = r * 2; C = 2 * r * Math.PI; A = rAtPow * Math.PI; rResult.innerHTML = "Raggio:" + " " + r; dResult.innerHTML = "Diametro:" + " " + d; CResult.innerHTML = "Circonferenza:" + " " + C; AResult.innerHTML = "Area:" + " " + A; } else if (d.length > 0) { r = d / 2; C = d + "π"; rAtPow = Math.pow(r, 2); A = rAtPow + "π"; rResult.innerHTML = "Raggio:" + " " + r; dResult.innerHTML = "Diametro:" + " " + d; CResult.innerHTML = "Circonferenza:" + " " + C; AResult.innerHTML = "Area:" + " " + A; } else if (C.length > 0) { r = C / 2; d = C; rAtPow = Math.pow(r, 2); A = rAtPow + "π"; rResult.innerHTML = "Raggio:" + " " + r; dResult.innerHTML = "Diametro:" + " " + d; CResult.innerHTML = "Circonferenza:" + " " + C + "π"; AResult.innerHTML = "Area:" + " " + A; } else if (A.length > 0) { r = Math.sqrt(A); d = r * 2; C = 2 * r + "π"; rResult.innerHTML = "Raggio:" + " " + r; dResult.innerHTML = "Diametro:" + " " + d; CResult.innerHTML = "Circonferenza:" + " " + C; AResult.innerHTML = "Area:" + " " + A + "π"; } } }

The script works, but only after reloading the page 1 or even 2 times.该脚本有效,但仅在重新加载页面 1 次甚至 2 次之后。 Hope you will solve my problem.希望你能解决我的问题。

The reason it isn't working is because the first part of your code, where you are reading the values the user has supplied, is run when the page is loaded, but not when the user click on the calc-button.它不工作的原因是因为你的代码的第一部分,你正在读取用户提供的值,在页面加载时运行,而不是在用户单击 calc-button 时运行。

The reason it works when you reload the page, is because the browser fills in the empty boxes with the values it had last time.当您重新加载页面时它起作用的原因是因为浏览器用上次的值填充了空框。

If you change the values and click on calc, you the calculation is done with the old values.如果更改值并单击 calc,则计算将使用旧值完成。

If you change this code:如果您更改此代码:

var r = document.getElementById("raggio").value;
var d = document.getElementById("diametro").value;
var C = document.getElementById("circonferenza").value;
var A = document.getElementById("area").value;

into this code:进入这个代码:

var input = {
  r : document.getElementById("raggio"),
  d : document.getElementById("diametro"),
  C : document.getElementById("circonferenza"),
  A : document.getElementById("area")
}

And then in the first part of your click-handler have this:然后在你的点击处理程序的第一部分有这个:

calc.onclick = function() {
  var
    r = input.r.value,
    d = input.d.value,
    C = input.C.value,
    A = input.A.value;

  // ... the rest of your code ...
}

Then you will actually read the values when you click in the calc-button.然后,当您单击计算按钮时,您将实际读取这些值。

There are other things to that should be changed in the code.代码中还有其他应该更改的内容。 For example you have var rAtPow = new Number();例如你有var rAtPow = new Number(); . . You don't need to allocate a number in javascript.您不需要在 javascript 中分配一个数字。 You can just declare the variable like var rAtPow;你可以像var rAtPow;一样声明变量var rAtPow; or var rAtPow=null;var rAtPow=null; or even var rAtPow=NaN;甚至var rAtPow=NaN; You also repeat yourself a lot to output the result.您还重复自己很多以输出结果。

I assume that your HTML code for the output looks something like:我假设您的输出 HTML 代码如下所示:

<div id="r"></div>
<div id="d"></div>
<div id="C"></div>
<div id="A"></div>

You could change it to something like this:你可以把它改成这样:

<div>Raggio: <output id="r"></div>
<div>Diametro: <output id="d"></div>
<div>Circonferenza: <output id="C"></div>
<div>Area: <output id="A"></div>

Then it is easy to translate without changing your code.然后很容易翻译而无需更改您的代码。 It also makes the code much cleaner.它还使代码更简洁。

I refactored you code a bit.我重构了你的代码。

HTML code (index.html) HTML 代码 (index.html)

<!doctype html>
<html>
  <head>
    <title>Stack overflow 32539264</title>
    <script src="index.js"></script>
  </head>
  <body>

    <div>
      <label>Raggio <input id="in_r"></label>
      <label>Diametro <input id="in_d"></label>
      <label>Circonferenza <input id="in_c"></label>
      <label>Area <input id="in_a"></label>
      <button id="calc">Calc</button>
    </div>

    <div>
      <div>Raggio: <output id="out_r"></div>
      <div>Diametro: <output id="out_d"></div>
      <div>Circonferenza: <output id="out_c"></div>
      <div>Area: <output id="out_a"></div>
    </div>

  </body>
</html>

and javascript (index.js)和 javascript (index.js)

document.addEventListener(
  "DOMContentLoaded",
  function(event) {
    var input = {
      r : document.getElementById("in_r"),
      d : document.getElementById("in_d"),
      C : document.getElementById("in_c"),
      A : document.getElementById("in_a")
    }

    var output = {
      r : document.getElementById("out_r"),
      d : document.getElementById("out_d"),
      C : document.getElementById("out_c"),
      A : document.getElementById("out_a")
    }

    var calc = document.getElementById("calc");
    calc.addEventListener(
      'click',
      function() {
        var
          r = input.r.value,
          d = input.d.value,
          C = input.C.value,
          A = input.A.value,
          rAtPow;

        if (r.length > 0) {
          rAtPow = Math.pow(r, 2);
          d = r * 2;
          C = 2 * r * Math.PI;
          A = rAtPow * Math.PI;
        } else if (d.length > 0) {
          r = d / 2;
          C = d + "π";
          rAtPow = Math.pow(r, 2);
          A = rAtPow + "π";
        } else if (C.length > 0) {
          r = C / 2;
          d = C;
          rAtPow = Math.pow(r, 2);
          A = rAtPow + "π";
        } else if (A.length > 0) {
          r = Math.sqrt(A);
          d = r * 2;
          C = 2 * r + "π";
        }
        output.r.value = r;
        output.d.value = d;
        output.C.value = C;
        output.A.value = A;
      }
    );
  }
);

Things to think about: there are no error handling in the code.需要考虑的事情:代码中没有错误处理。 You could make it more clear that you only use one of the input values by clearing the values that aren't used.您可以通过清除未使用的值来更清楚地表明您只使用其中一个输入值。

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

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