此代碼不能工作,請幫助!ArrayIndexOutOfBound使用for循環時出現異常
ArrayIndexOutOfBoundException由編譯器拋出,請幫助!
for(var i=1;i<='<%=count%>';+++)
{
var x = document.getElementById("person");
var opt = document.createElement("option");
opt.text = '<%=list[i]%>'; <------------------ ERROR IS AT THIS LINE
opt.value = '<%=list[i]%>';
x.add(opt);
}
}
你確定它應該是'I'='<%=count%>''而不是'我< '<%=count%>'? – Manu
這是隱藏在'javascript'標籤後面的** Java **問題。 JavaScript不**具有**'ArrayOutOfBoundsException'。 ** Java **。它們是不同的語言,具有不同(儘管相似)的語法,以及不同(不是非常相似)的操作模型。如果我們假設你的Java代碼有'list',它是一個**數組**(不是一個列表),而'count'來自'list.length',那麼這是[這個問題的重複](http: //stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it)。 –
好吧,大部分。你也在你的JSP代碼中(在服務器上,因爲頁面已準備好發送給客戶端)假設'i'會在你的JavaScript代碼中(在客戶端,稍後,當頁面被加載並運行客戶端代碼)。 –