将javascript变量分配给jsp代码? [英] assign javascript variable to jsp code?
问题描述
我在jsp中有4个arraylist,我想从javascript函数中获取列表元素.
I have 4 arraylist in jsp and I want to get elements of lists from javascript function.
从jsp arraylist中获取一个元素似乎没有问题.
it looks like there is no problem getting one element from jsp arraylist.
但是我不知道如何将多个或所有元素从jsp arraylist移到javascript代码.
But I don't know how to move multiple or all elements from jsp arraylist to javascript code.
下面是我的代码
<script>
function makeTable(){
for(c=0;c<row_num;c++){
row[c]=document.createElement('tr');
for(k=0;k<cell_num;k++) {
cell[k]=document.createElement('td');
cont = document.createElement('a');
cont.href="./bbs_view.jsp?count=" + c;
cont.innerHTML ="<%=title.get(0)%>";
cell[k].appendChild(cont);
row[c].appendChild(cell[0]);
}
}
}
</script>
如您所见,
上面的函数只能从jsp arraylist中获取一个元素.
有什么方法可以将<%= title.get(0)%>替换为
像<%= title.get(javascript var c)%>之类的东西?
as you can see, above function can get only one element from jsp arraylist.
is there any way to replace <%=title.get(0)%> to
something like <%=title.get( javascript var c)%>?
推荐答案
在您的脚本片段中发出一个javascript数组(假定标题是String的java集合),并使用数组的js对应物.
Emit a javascript array within your scriplet (assumin titles is a java collection of Strings) and use the js counterpart of your array.
<script>
var jsArray = [];
<% int i = 0; foreach (String iterat : titles) { %>
jsArray[<%= i %>] = '<%= iterat %>';
<% i++; } %>
</script>
几乎没有意思(最好在自定义标签中使用此标签,但应该可以使用).
Is roughly unelegant (better have this in a custom tag, but should work).
注意:即时输入,可以在^^
BEWARE: typed on the fly, can have syntax typos in ^^
BigMike
这篇关于将javascript变量分配给jsp代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!