[英]JavaScript: How to read an input id with a jsp tag inside (id=“studenttid_<%=intCount%>”)
Let say I have 10 students. 假设我有10个学生。
I declared a variable, intCount=0
so that I can loop the input field with their respective id. 我声明了一个变量
intCount=0
以便可以使用各自的ID循环输入字段。
The code for the looping input field looks like this: 循环输入字段的代码如下所示:
<%intCount++%>
<tr>
<td><input type="text" id="studenttid_<%=intCount%>"%>"></td>
</tr>
After compilation, the output is: 编译后,输出为:
studentid_1 studentid_1
studentid_2 studentid_2
studentid_3 studentid_3
studentid_4 studentid_4
studentid_5 studentid_5
studentid_6 studentid_6
studentid_7 studentid_7
studentid_8 studentid_8
studentid_9 studentid_9
studentid_10 studentid_10
Then, I need to parse the studentid_1,...,studentid_10 to my javascript, which the problem is I do not know what should I write in document.getElementById('')
. 然后,我需要将studentid_1,...,studentid_10解析为我的javascript,问题是我不知道应该在
document.getElementById('')
写什么。
I tried something like: 我尝试了类似的东西:
<script>var studentId = document.getElementById('studentid_<%=intCount%>')</script>
but it returns "variable intCount is undefined". 但它返回“变量intCount未定义”。 What should I do to solve my problem?
我该怎么做才能解决我的问题? Please help.
请帮忙。 Thanks in advance.
提前致谢。
You can do the following: 您可以执行以下操作:
You could do this with .reduce
instead of .filter().map()
but this is probably simpler to understand. 你可以用这样做
.reduce
,而不是.filter().map()
但是这可能是更容易理解。
var inputs = document.getElementsByTagName("input"); var inputArr = Array.apply(null, inputs); var studenttid_elements = inputArr.filter(ele => ele.id.indexOf("studenttid_") === 0); var studenttid_ids = studenttid_elements.map(ele => ele.id); console.log("Elements:", studenttid_elements); console.log("IDs", studenttid_ids);
<input type="text" id="studenttid_1"> <input type="text" id="studenttid_2"> <input type="text" id="other_id">
You need to keep your JavaScript code within a <script>
tag in a JSP file or in a file included in a JSP file. 您需要将JavaScript代码保留在JSP文件或JSP文件中包含的文件的
<script>
标记内。
<script>var studentId = document.getElementById('studenttid_<%=intCount%>') </script>
If your JavaScript is not within a tag or not within a JSP file, the Jasper engine will ignore it. 如果您的JavaScript不在标记中或JSP文件中,则Jasper引擎将忽略它。
Alternately, just use EL. 或者,只需使用EL。
'studenttid_${intCount}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.