我做了一個程序,詢問用戶他要顯示多少個輸入欄,然後用戶必須在輸入欄中輸入一些值並點擊驗證,但是我遇到的問題是,只要我點擊確認,沒有任何反應。我得到的錯誤:Javascript:輸入欄不工作或給出任何結果
Uncaught TypeError: Cannot read property 'value' of null
每當我點擊驗證。
這裏是我的代碼:
<html>
<head>
<script language="javascript">
var x = [];
var choose;
var i;
var xar=[];
choose=parseFloat(prompt("how many inputs u want to display"));
for(i=0;i<choose;i++)
x[i]=document.getElementById("cont"+i).style.visibility="visible";
function hola() {
for(i=0;i<choose;i++)
document.getElementById("cont"+i).style.visibility="visible";
}
function verify(){
for(i=0;i<choose;i++){
xar[i]=document.getElementById("userNumber"+i).value;
}
for(i=0;i<choose;i++)
if(xar[i]==i)
alert("good");
else
alert("wrong");
}
</script>
</head>
<body>
<input type="button" onclick="hola()" value="hidefield0" id="boton0">
<div id="cont0" style="visibility: hidden;">
<input type="text" id="userNumber" class="something">
</div>
<input type="button" onclick="hola()" value="hidefield2" id="boton1">
<div id="cont1" style="visibility: hidden;" class="something">
<input type="text" id="username2">
</div>
<input type="button" onclick="hola()" value="hidefield1" id="boton2">
<div id="cont2" style="visibility: hidden;" class="something">
<input type="text" id="username3">
</div>
<input type="button" onclick="hola()" value="hidefield3" id="boton3">
<div id="cont3" style="visibility: hidden;" class="something">
<input type="text" id="username4">
</div>
<input type="button" onclick="hola()" value="hidefield4" id="boton4">
<div id="cont4" style="visibility: hidden;" class="something">
<input type="text" id="username5">
</div>
<input type="button" onclick="hola()" value="hidefield5" id="boton5">
<div id="cont5" style="visibility: hidden;" class="something">
<input type="text" id="username6">
</div>
<input type="button" id="verifyBtn" value="verify" onclick="verify()">
</body>
<html>
是否有您所使用的任何HTML? – Shniper
是的抱歉忘了把它放在..掛在我會做到這一點 –
@Suniper我包括的HTML :)謝謝你幫助順道。你認爲我在代碼中做錯了什麼? :/ –