admin管理员组

文章数量:1425749

I have an array of values that I want to pass in as a parameter to a 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. Their names need to correspond to the values in the array. That is, in my js function I have a loop for:

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

Is this possible?

I have an array of values that I want to pass in as a parameter to a 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. Their names need to correspond to the values in the array. That is, in my js function I have a loop for:

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

Is this possible?

Share Improve this question edited Aug 16, 2011 at 18:13 Bozho 598k147 gold badges1.1k silver badges1.2k bronze badges asked Aug 16, 2011 at 17:51 JohnJohn 131 silver badge3 bronze badges 5
  • 1 Sorry, but I'm not using JQuery. – John Commented Aug 16, 2011 at 17:57
  • Doesn't matter jQuery is JavaScript is as well. The solution I gave there can be applied to your problem too. – Felix Kling Commented Aug 16, 2011 at 18:00
  • 1 @Felix Kling - I hope that's not really waht you think "jQuery is JavaScript". This is like saying C++ is ATL or MFC, or that C++ is C, or that HTML is XML. jQuery is OF javaScript. jQuery IS NOT JavaScript. – Brian Commented Aug 16, 2011 at 18:18
  • @Brian: Keeping in mind that jQuery is a library and JavaScript a language, your C++/C and HTML/XML analogies are not correct imo. Anyway, that is just the way I express myself. If I have a function foo written in JavaScript, I also say, "this is a JavaScript function" or "this function is JavaScript". Maybe it's because I'm not a native English speaker. I'm sorry if it causes any confusion. – Felix Kling Commented Aug 16, 2011 at 18:36
  • 2 Has very little to do with native speaking - you understood his objection to your issue. You also understood my analogies. HTML is a subset of XML. C++ is built on C. And on the language issue, saying jQuery is JavaScript is just like saying French, Italian and English are latin. They are all of latin. None of them is latin. Okay that may be a stretch, but I found it appropriate based on the discussion :) – Brian Commented Aug 16, 2011 at 18:40
Add a ment  | 

1 Answer 1

Reset to default 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 remended way of writing code in JSP. If you want to write scriptlets, it will be very similar - with a for (String item : arr) { .. }

本文标签: htmlPass array from JSP as parameter to JavaScript functionStack Overflow