[英]Pass array from JSP as parameter to JavaScript function
I have an array of values that I want to pass in as a parameter to a Javascript function.我有一个值数组,我想将它们作为参数传递给 Javascript function。 For instance, something like:
例如,类似:
<% ArrayList<String> arr = NodeUtil.getValues(); %>
<input type="button" name="submit" value="Add" onClick="addTextBox('<%= all values in arr %>')"/>
I'm trying to dynamically add textboxes to a div.我正在尝试将文本框动态添加到 div。 Their names need to correspond to the values in the array.
它们的名称需要与数组中的值相对应。 That is, in my js function I have a loop for:
也就是说,在我的 js function 我有一个循环:
newdiv.innerHTML = "<input type=\"text\" style=\"width: 235px\" name=\+each value in arr +"\" />
Is this possible?这可能吗?
The solution in the linked question is good, but if you don't want an external library:链接问题中的解决方案很好,但是如果您不想要外部库:
var array = new Array();
<c:forEach items="${jspArray}" var="item">
array.push("${item}");
</c:forEach>
This is ustil JSTL and EL, which is the recommended way of writing code in JSP.这是ustil JSTL和EL,这是JSP中推荐的写代码方式。 If you want to write scriptlets, it will be very similar - with a
for (String item: arr) {.. }
如果你想编写 scriptlet,它会非常相似 - 使用
for (String item: arr) {.. }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.