我的JavaScript无法处理我的HTML代码

时间:2017-01-26 22:34:05

标签: javascript html

我试图制作抵押贷款计算器。但我不知道问题是什么,所以它不起作用。你能告诉我这是什么问题吗?

这是我的html代码



<!DOCTYPE html>
<html>
<head>
   <meta charset="utf-8" />
   <meta name="viewport" content="width=device-width,initial-scale=1.0">
   <title>Mortgage calculator</title>
   
   <link rel="stylesheet" href="styles.css" />
   <script src="assignment1.js"></script>

</head>

<body>
   <header>
      <h1>
         Mortgage calculator
      </h1>
   </header>

   <article>
      <h2><br></h2>
      <form>
      		<fieldset>
      		<label>
      		Mortgage Amount: $
            	<input type="number" id="MA" name="MA" required>
			</label>

      		<label>
      		Down Payment Option: 
      		 <input name="pickone" type="radio" id="5" onclick="document.getElementById('amor').value = '25';" required>5%
      		 <input name="pickone" type="radio" id="10" onclick="document.getElementById('amor').value = '25';" required>10%
      		 <input name="pickone" type="radio" id="15" onclick="document.getElementById('amor').value = '25';" required>15%
      		 <input name="pickone" type="radio" id="20" onclick="document.getElementById('amor').value = '30';" required>20%
      		</label>    	
      			
      		<label>
      		Down Payment: $
      			<input type="number" id="DP" name="DP" required>
 			</label>
 			
      		<label>
      		Amortization: 
      			<input type="text" id="amor" name="amor" required> years
      		</label>  
      
      		<label>
      		Interest Rate: 
      			<input type="number" id="IR" name="IR" required>%
      		</label>
      	
			<button type="button" id="button">Calculate</button>

			<label>
			Result: 
				<input type="number" name="Result" id="Result">
			</label>
			
			</fieldset>
			</form>
   </article>   	
</body>
</html>
&#13;
&#13;
&#13;

这是我的assignment1.js文件

&#13;
&#13;
 function calculate() {
   		var mortamount = document.getElementById("MA").value;
   		var downpayment = document.getElementById("DP").value;
   		var amortization = document.getElementById("amor").value;
   		var interest = document.getElementById("IR").value;
   		
   	
   		var p = mortamount - downpayment;
   		var n = amortization * 12;
   		var r = interest / 100 / 12;
   		var 1R = 1 + r;
   		
   		
   		var top = r * p * Math.pow(1r,n);
   		var bot = Math.pow(1r,n) - 1;
   		
   		var mpay = top / bot;
   		
   		document.getElementById("Result").value = mpay;
	}
	document.getElementById("button").
	addEventListener("click", calculate, false);
&#13;
&#13;
&#13;

感谢您的帮助:)。

2 个答案:

答案 0 :(得分:1)

检查开发者控制台是否有错误... var 1R,变量名称不能以数字开头。

答案 1 :(得分:0)

我运行了你的代码,11号线,1R似乎造成了一些问题。变量名称不应以数字开头。 (它也有1R,然后是1r所以我认为你需要先清除变量名。)然后,就像其他人评论的那样,首先加载javascript文件并查找尚未加载的按钮。因此,您需要在js文件中添加如下内容。

document.addEventListener('DOMContentLoaded', () => {
// your code goes here
}