点击按钮后没有响应 (javascript)

huangapple go评论66阅读模式
英文:

No response upon clicking button (javascript)

问题

我正在构建一个在线计算器,但在同一页面上显示结果方面遇到了很多问题。

为什么我的结果不显示,我做错了什么?

理想情况是用户输入4个信息,然后执行逻辑,然后在页面下方显示答案...?

以下是你提供的代码:

<div class="container">
  <h1>Calc</h1>
  <form id="calculator">
    <label for="name">Name:</label>
    <input type="text" id="name" name="name" required>

    <label for="age">Age:</label>
    <input type="number" id="age" name="age" min="0" max="120" required>

    <label for="yearsToRetire">Years Until Retirement:</label>
    <input type="number" id="yearsToRetire" name="yearsToRetire" min="1" max="50" required>

    <label for="passiveIncome">Desired Passive Income in Retirement:</label>
    <input type="number" id="passiveIncome" name="passiveIncome" min="0" required>

    <button onclick="calculate()">Calculate</button>
  </form>

  <div id="results"></div>
</div>

请指出,什么部分需要翻译。

英文:

I am building an online calculator, and am having a lot of trouble getting results to display on the same page.

What am I doing wrong that the results are not displaying?

The ideal scenario is the user inputs the 4 pieces of info, then the logic is performed, and the answers are displayed below the page...?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function calculate(event) {
  event.preventDefault(); // prevent form submission from reloading the page

  // Get input values
  const name = document.getElementById(&#39;name&#39;).value;
  const age = parseInt(document.getElementById(&#39;age&#39;).value);
  const yearsToRetire = parseInt(document.getElementById(&#39;years-to-retire&#39;).value);
  const passiveIncome = parseInt(document.getElementById(&#39;passive-income&#39;).value);

  // Calculate results
  const inflationRate = 0.025;
  const yearsToInvest = yearsToRetire - (age &gt;= 60 ? 0 : yearsToRetire);
  const passiveIncomeTarget = passiveIncome / 0.95;
  const futureValueTarget = passiveIncomeTarget * ((1 + inflationRate) ** yearsToInvest);

  // Prepare result strings
  const passiveIncomeTargetFormatted = formatCurrency(passiveIncomeTarget);
  const futureValueTargetFormatted = formatCurrency(futureValueTarget);

  // Display results
  const resultsDiv = document.getElementById(&#39;results&#39;);
  resultsDiv.innerHTML = `
    &lt;h2&gt;Results for ${name}&lt;/h2&gt;
    &lt;p&gt;Your target passive income is &lt;strong&gt;${passiveIncomeTargetFormatted}&lt;/strong&gt; per year.&lt;/p&gt;
    &lt;p&gt;To achieve this, you need to have a total of &lt;strong&gt;${futureValueTargetFormatted}&lt;/strong&gt; in investments.&lt;/p&gt;
  `;
}

function formatCurrency(amount) {
  return &#39;$&#39; + amount.toLocaleString(&#39;en-US&#39;, {
    minimumFractionDigits: 0,
    maximumFractionDigits: 0
  });
}

const form = document.getElementById(&#39;calculator&#39;);
form.addEventListener(&#39;submit&#39;, calculate);

<!-- language: lang-css -->

body {
  font-family: Arial, sans-serif;
}

.container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  text-align: left;
}

h1 {
  text-align: center;
  font-size: 36px;
  margin-bottom: 30px;
}

form {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-gap: 20px;
  margin-bottom: 30px;
}

label {
  font-size: 18px;
  margin-bottom: 5px;
}

input[type=&quot;number&quot;],
input[type=&quot;text&quot;] {
  font-size: 18px;
  padding: 5px;
  border-radius: 5px;
  border: 1px solid #ccc;
  width: 100%;
}

button[type=&quot;submit&quot;] {
  font-size: 18px;
  background-color: #4CAF50;
  color: #fff;
  padding: 10px 20px;
  border-radius: 5px;
  border: none;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

button[type=&quot;submit&quot;]:hover {
  background-color: #3e8e41;
}

#results {
  display: none;
  margin-top: 30px;
  font-size: 24px;
}

#results p {
  margin: 10px 0;
}

#results table {
  width: 100%;
  margin-top: 20px;
  border-collapse: collapse;
}

#results th,
#results td {
  padding: 10px;
  border: 1px solid #ccc;
}

#results th {
  background-color: #f2f2f2;
  font-weight: bold;
  text-align: left;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;h1&gt;Calc&lt;/h1&gt;
  &lt;form id=&quot;calculator&quot;&gt;
    &lt;label for=&quot;name&quot;&gt;Name:&lt;/label&gt;
    &lt;input type=&quot;text&quot; id=&quot;name&quot; name=&quot;name&quot; required&gt;

    &lt;label for=&quot;age&quot;&gt;Age:&lt;/label&gt;
    &lt;input type=&quot;number&quot; id=&quot;age&quot; name=&quot;age&quot; min=&quot;0&quot; max=&quot;120&quot; required&gt;

    &lt;label for=&quot;yearsToRetire&quot;&gt;Years Until Retirement:&lt;/label&gt;
    &lt;input type=&quot;number&quot; id=&quot;yearsToRetire&quot; name=&quot;yearsToRetire&quot; min=&quot;1&quot; max=&quot;50&quot; required&gt;

    &lt;label for=&quot;passiveIncome&quot;&gt;Desired Passive Income in Retirement:&lt;/label&gt;
    &lt;input type=&quot;number&quot; id=&quot;passiveIncome&quot; name=&quot;passiveIncome&quot; min=&quot;0&quot; required&gt;

    &lt;button onclick=&quot;calculate()&quot;&gt;Calculate&lt;/button&gt;
  &lt;/form&gt;

  &lt;div id=&quot;results&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 0

  1. 从按钮中删除onclick属性,并将按钮的type属性更改为submit
  2. 具有id years-to-retirepassive-income 的元素不存在。请改用这些选择器 yearsToRetirepassiveIncome

尝试此代码。

英文:
  1. Remove the onclick attribute from the button and change the buttons type attribute to submit.
  2. Elements with id years-to-retire and passive-income doesn't exist. Use these selectors yearsToRetire and passiveIncome instead.

Try this

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function formatCurrency(amount) {
  return &#39;$&#39; + amount.toLocaleString(&#39;en-US&#39;, {
    minimumFractionDigits: 0,
    maximumFractionDigits: 0
  });
}

document.getElementById(&#39;calculator&#39;).addEventListener(&#39;submit&#39;, function(event) {
  event.preventDefault(); // prevent form submission from reloading the page

  // Get input values
  const name = document.getElementById(&#39;name&#39;).value;
  const age = parseInt(document.getElementById(&#39;age&#39;).value);
  const yearsToRetire = parseInt(document.getElementById(&#39;yearsToRetire&#39;).value);
  const passiveIncome = parseInt(document.getElementById(&#39;passiveIncome&#39;).value);

  // Calculate results
  const inflationRate = 0.025;
  const yearsToInvest = yearsToRetire - (age &gt;= 60 ? 0 : yearsToRetire);
  const passiveIncomeTarget = passiveIncome / 0.95;
  const futureValueTarget = passiveIncomeTarget * ((1 + inflationRate) ** yearsToInvest);

  // Prepare result strings
  const passiveIncomeTargetFormatted = formatCurrency(passiveIncomeTarget);
  const futureValueTargetFormatted = formatCurrency(futureValueTarget);

  // Display results
  const resultsDiv = document.getElementById(&#39;results&#39;);
  resultsDiv.innerHTML = `
  	    &lt;h2&gt;Results for ${name}&lt;/h2&gt;
  	    &lt;p&gt;Your target passive income is &lt;strong&gt;${passiveIncomeTargetFormatted}&lt;/strong&gt; per year.&lt;/p&gt;
  	    &lt;p&gt;To achieve this, you need to have a total of &lt;strong&gt;${futureValueTargetFormatted}&lt;/strong&gt; in investments.&lt;/p&gt;
  	  `;
});

<!-- language: lang-css -->

body {
  font-family: Arial, sans-serif;
}

.container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  text-align: left;
}

h1 {
  text-align: center;
  font-size: 36px;
  margin-bottom: 30px;
}

form {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-gap: 20px;
  margin-bottom: 30px;
}

label {
  font-size: 18px;
  margin-bottom: 5px;
}

input[type=&quot;number&quot;],
input[type=&quot;text&quot;] {
  font-size: 18px;
  padding: 5px;
  border-radius: 5px;
  border: 1px solid #ccc;
  width: 100%;
}

button[type=&quot;submit&quot;] {
  font-size: 18px;
  background-color: #4CAF50;
  color: #fff;
  padding: 10px 20px;
  border-radius: 5px;
  border: none;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

button[type=&quot;submit&quot;]:hover {
  background-color: #3e8e41;
}

#results {
  /*  	  display: none;*/
  margin-top: 30px;
  font-size: 24px;
}

#results p {
  margin: 10px 0;
}

#results table {
  width: 100%;
  margin-top: 20px;
  border-collapse: collapse;
}

#results th,
#results td {
  padding: 10px;
  border: 1px solid #ccc;
}

#results th {
  background-color: #f2f2f2;
  font-weight: bold;
  text-align: left;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;h1&gt;Calc&lt;/h1&gt;
  &lt;form id=&quot;calculator&quot;&gt;
    &lt;label for=&quot;name&quot;&gt;Name:&lt;/label&gt;
    &lt;input type=&quot;text&quot; id=&quot;name&quot; name=&quot;name&quot; required&gt;

    &lt;label for=&quot;age&quot;&gt;Age:&lt;/label&gt;
    &lt;input type=&quot;number&quot; id=&quot;age&quot; name=&quot;age&quot; min=&quot;0&quot; max=&quot;120&quot; required&gt;

    &lt;label for=&quot;yearsToRetire&quot;&gt;Years Until Retirement:&lt;/label&gt;
    &lt;input type=&quot;number&quot; id=&quot;yearsToRetire&quot; name=&quot;yearsToRetire&quot; min=&quot;1&quot; max=&quot;50&quot; required&gt;

    &lt;label for=&quot;passiveIncome&quot;&gt;Desired Passive Income in Retirement:&lt;/label&gt;
    &lt;input type=&quot;number&quot; id=&quot;passiveIncome&quot; name=&quot;passiveIncome&quot; min=&quot;0&quot; required&gt;

    &lt;button type=&quot;submit&quot;&gt;Calculate&lt;/button&gt;
  &lt;/form&gt;

  &lt;div id=&quot;results&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月26日 19:07:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定