讓我說我有10名學生。JavaScript:如何讀取一個輸入id與一個jsp標籤裏面(id =「studenttid_ <%=intCount%>」)
我聲明瞭一個變量,intCount=0
,這樣我就可以循環輸入字段和它們各自的ID。
用於循環輸入字段中的代碼看起來是這樣的:
<%intCount++%>
<tr>
<td><input type="text" id="studenttid_<%=intCount%>"%>"></td>
</tr>
編譯後,輸出爲:
studentid_1
studentid_2
studentid_3
studentid_4
studentid_5
studentid_6
studentid_7
studentid_8
studentid_9
studentid_10
然後,我需要解析studentid_1,...,studentid_10到我的javascript,問題是我不知道我應該在document.getElementById('')
寫什麼。
我想是這樣的:
<script>var studentId = document.getElementById('studentid_<%=intCount%>')</script>
但它返回 「變量intCount是不確定的」。我該怎麼做才能解決我的問題?請幫忙。提前致謝。
嗨@Goblinlod,感謝您的迴應。我不明白爲什麼用1和2聲明輸入ID。它應該自動循環,直到達到最後一個ID。例如,如果我有100個學生ID,那麼它應該循環直到studentid_100並解析爲javascript。 –
循環創建字段似乎不是你的問題真正涉及到。此外,他們不提供任何使用這種類型的模板(stackoverflow)。所以我省略了它。我只是把一些JavaScript,你可以使用,無論這種情況下。儘管在你的例子中你使用'studenttid_#'而不是'studentid_#'。我認爲這是一個錯誤,但我的代碼使用第一個。 – Goblinlord
此外,我想這將取決於你想用'var studentid'做什麼。上面的代碼爲您提供了所有這些javascript元素的數組(它實際上是可運行的)。 – Goblinlord