将JSP中的数组作为参数传递给JavaScript函数

时间:2021-01-18 11:58:23

I have an array of values that I want to pass in as a parameter to a Javascript function. For instance, something like:

我有一个值数组,我想作为参数传递给Javascript函数。例如,类似于:

<% 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. Their names need to correspond to the values in the array. That is, in my js function I have a loop for:

我正在尝试动态地将文本框添加到div。它们的名称需要与数组中的值相对应。也就是说,在我的js函数中,我有一个循环:

newdiv.innerHTML = "<input type=\"text\" style=\"width: 235px\" name=\+each value in arr +"\" />

Is this possible?

这可能吗?

1 个解决方案

#1


4  

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. If you want to write scriptlets, it will be very similar - with a for (String item : arr) { .. }

这是ustil JSTL和EL,这是在JSP中编写代码的推荐方法。如果你想编写scriptlet,它将非常相似 - 使用for(String item:arr){..}

#1


4  

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. If you want to write scriptlets, it will be very similar - with a for (String item : arr) { .. }

这是ustil JSTL和EL,这是在JSP中编写代码的推荐方法。如果你想编写scriptlet,它将非常相似 - 使用for(String item:arr){..}