Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在HTML中显示JS计算的结果_Javascript_Html_Display - Fatal编程技术网

Javascript 在HTML中显示JS计算的结果

Javascript 在HTML中显示JS计算的结果,javascript,html,display,Javascript,Html,Display,JavaScript的新特性。我正在尝试创建一个非常基本的抵押计算器,作为我的第一个小JavaScript项目。这个想法是显示每月的抵押贷款支付成本 我现在没有任何错误。console.log显示的是正确答案,但我希望它显示在submit按钮旁边的框中。我找不到在该框中显示计算结果的方法。我浏览了互联网,我绞尽脑汁想怎么做。 非常感谢您的帮助 这是我的密码: HTML <form action=""> <input type="number&q

JavaScript的新特性。我正在尝试创建一个非常基本的抵押计算器,作为我的第一个小JavaScript项目。这个想法是显示每月的抵押贷款支付成本

我现在没有任何错误。console.log显示的是正确答案,但我希望它显示在submit按钮旁边的框中。我找不到在该框中显示计算结果的方法。我浏览了互联网,我绞尽脑汁想怎么做。 非常感谢您的帮助

这是我的密码:

HTML

<form action="">
  <input type="number" id="amountBorrowed" placeholder="Amount Borrowed">
  <input type="number" id="mortgageTerm" placeholder="Term in Years">
  <input type="button" id="sub" value="Submit" onclick="mortgageCalc()">
  <input type="number" id="monthlyCost" name="monthlyCost">  
</form>

非常感谢。

结果不需要使用输入。如果对结果使用输入,则用户可以编辑该字段

函数mortgageCalc(){
var借款金额;
抵押期限;
var月成本;
var result=document.getElementById('monthlyCost')
amountbrooked=document.getElementById(“amountbrooked”).value;
mortgageTerm=document.getElementById(“mortgageTerm”).value;
抵押期限*=12;
月成本=(借款金额/抵押期限);
控制台日志(月成本);
result.innerHTML=monthlyCost
}

结果:


我对另一个版本感到厌烦,如果还需要考虑利息的话,我会使用an来计算月利率:

document.querySelector('form')。addEventListener('input',ev=>{
if(ev.target.tagName==“输入”){
常量[amnt,yrs,intr]=“借款金额,抵押期限,年利息”。拆分(',)
.map(id=>+document.getElementById(id).value);
设a,n=yrs*12;//月数
如果(intr>0){
设q=(1.+intr/100)**(1/12);//月利率+1
a=amnt*q**n*(q-1)/(q**n-1);.//年金公式
}else{a=amnt/n;}
document.getElementById('monthlyCost')。textContent=a.toFixed(2)
}
})
input{width:150px}

结果:

document.getElementById(“monthlyCost”).value=monthlyCost-您可能希望学习您的互联网搜索技术。
function mortgageCalc() {
  var amountBorrowed;
  var mortgageTerm;
  var monthlyCost;
  amountBorrowed = document.getElementById("amountBorrowed").value;
  mortgageTerm = document.getElementById("mortgageTerm").value;
  mortgageTerm *= 12;      
  monthlyCost = (amountBorrowed / mortgageTerm);
   console.log(monthlyCost);
}