我正在製作一個包含兩個數字字段的html文檔。document.getElementById似乎返回null
用戶輸入數字信息,然後單擊一個按鈕激活單獨的.js文件中的方法。我遇到的問題是,每當您輸入數據並按下按鈕時,它將返回數據爲NULL
。
以下是我有:
<html>
<head>
</head>
<body>
<h1>Retirement calculation page!</h1>
<p class="a">Enter the information below to find out how much money you will have for retirement</p>
<form name="form1">
Enter Age: <input type ="number" name="age1" min="1" max="125">
</form>
<form name="form2">
Enter Contribution amount: <input type="number" name="cont1">
</form>
<button onclick="rFunction()">Submit</button>
<script src="retirement.js"></script>
</body>
</html>
這裏是在的.js文件的方法。
function rFunction()
{
var age = document.getElementById("age1");
var cont = document.getElementById("cont1");
document.write("Age = " + age);
document.write("Cont = " + cont);
}
我在做什麼錯?
您可以使用'getElementById',但是沒有任何元素實際上有ID。 – Blender
[爲什麼jQuery或DOM方法如\ getElementById \'找不到元素?](http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom- method-such-as-getelementbyid-not-find-the-element) –
@FelixKling:不是重複的,而是一個簡單的錯誤。 –