财务规划报价计算器

Financial Planning Price Quote Calculator

经过大量令人困惑的研究,我已经放弃了为我正在从事的项目编写代码的尝试,我希望有人能帮助我。

我需要为客户的网站创建一个 HTML 报价块。公式需要能够做到这一点:

(Income * 0.01) + ((Property Value + Investments - Debt)*0.005)

因此变量是收入、属性 价值、投资和债务。输出需要以美元 ($) 为单位,并且应该至少为 150。换句话说,如果有人输入小于 150 的值,它应该显示 150。

下面是我的尝试。
怎么了?

function calculatePrice(myform){

  //Get selected data 
  var elt = document.getElementById(“income");
  var elt = document.getElementById(“property");
  var elt = document.getElementById(“investments");
  var elt = document.getElementById("debt");
    

    
  //convert data to integers
  income = parseInt(income);
  property = parseInt(property);
  investments = parseInt(investments);
  investments = parseInt(debt);
    
  //calculate total value  
var result=Math.max(income * 0.01 + (property + investments - debt)*0.005, 150);
    
  //print value to  PicExtPrice 
  document.getElementById("PicExtPrice").value=total;

}
<FORM Name="myform">

  <div>
    <label for="income">Yearly Income</label>
    <input id="income" type="text">
  </div>

  <div>
    <label for="property">Estimated Value of your Asstes (house, land, etc.)</label>
    <input id="property" type="text">
  </div>

  <div>
    <label for="investments">Estimated Value of Investments (stocks, bonds, IRA, etc.)</label>
    <input id="investments" type="text">
  </div>

  <div>
    <label for="debt">Estimated Debt (mortgage, loans, credit cards)</label>
    <input id="debt" type="text">
  </div>

</FORM>

<button type="button" onclick="calculatePrice()">Calculate</button> The new calculated price:
<INPUT type="text" id="PicExtPrice" Size=8>

View on JSFiddle

基本公式应如下所示:

var result=Math.max(Income * 0.01 + (PropertyValue + Investments - Debt)*0.005, 150);

假设变量已经包含来自您相关输入字段的数值。您仍然需要确保每次更改一个输入字段(或每次释放一个键)时都会触发此计算,并将结果写回 HTML 页面上的合适位置。所有这些都可以通过一些 jquery 命令轻松完成。

将其写回您的页面时,您可能还应该使用 result.toFixed(2).

之类的方法将结果四舍五入到小数点后两位

编辑

我已将您的 JSfifddle 转移到内部 SO-fiddle。您的代码有几个小问题。查看以下内容并找出不同之处:

function getv(id){return parseInt(document.getElementById(id).value||0);}
function calculatePrice(myform){
  // calculate result:
  var result=Math.max(getv('income') * 0.01 + (getv('property') 
           + getv('investments') - getv('debt'))*0.005, 150);
  // place the output into the target field:
  document.getElementById("PicExtPrice").value=result;

}
<FORM Name="myform">

<div>
 <label for="income">Yearly Income</label>
 <input id="income" type="text">
</div>

<div>
 <label for="property">Estimated Value of your Asstes (house, land, etc.)</label>
 <input id="property" type="text">
</div>

<div>
 <label for="investments">Estimated Value of Investments (stocks, bonds, IRA, etc.)</label>
 <input id="investments" type="text">
</div>

<div>
 <label for="debt">Estimated Debt (mortgage, loans, credit cards)</label>
 <input id="debt" type="text">
</div>

</FORM>

<button type="button" onclick="calculatePrice()">Calculate</button>

The new calculated price:<INPUT type="text" id="PicExtPrice" Size=8>

为了让事情更简单,我定义了一个小 "getter" 函数来检索输入字段值。我只是讨厌在编码时重复自己。因此,getv() returns 给定的任何输入 ID 的浮点值。公式没有改变,但在你的最后一行你试图引用一个未定义的变量 total,而它应该是 result.

另一个问题可能是一些引号,例如 document.getElementById(“income")。注意:第一个 是错误的,应该是普通的 "

最终编辑:

为了使计算多一点 "forgiving" 我在 document.getElementById(id).value 之后添加了 ||00 现在只要输入字段未被触及(并返回一个空字符串)就会介入。我忍不住再次缩短整个事情。谁需要 incomeproperty 等的各个变量?这只是不必要的绒毛,当主要公式可以直接使用 getv() 函数编写时。