如何在单个jsp页面中访问javascript函数中的jsp数组?
String[] abc[]={"saab","volvo","bmw","Benz","Porsche","Jaguar"};
abc[0]="saab";
答案 0 :(得分:0)
您应首先使用例如FlexJson将其序列化为json对象。
您可以将java数组序列化为json数组,该数组应该看起来像
[{item1, item2},{item},{item1,item2}]
使用该方法
String[] temp = ...
StringBuilder builder = new StringBuilder();
builder.append("[");
for(int i = 0 ; i < temp.length ; i++){
builder.append(temp[i]);
if(i != temp.length - 1 ){
builder.append(",");
}
}
builder.append("]");
String jsonArray = builder.toString();
并将其添加到您的请求中;
然后在你的javascript使用
var array = <%= request.getAttribute("temp")%>;
检查是否是
console.log(array);
答案 1 :(得分:0)
按字面意思打印它并通过javascript处理它。
背后的场景: jsp-inline代码在服务器端运行。 javasrcript代码在浏览器上运行..
答案 2 :(得分:-1)
<%
String[] abc={'saab',' volvo',"bmw","Benz","Porsche","Jaguar"};
%>
<script language="JavaScript">
var jsArr = new Array();
<%
for (int i=0; i < abc.length; i++) {
%>
jsArr[<%= i %>] = '<%=abc[i] %>'; //Here is the latest update check it sravan .Put single quotes.
<%}%>
</script>
function myFunction()
{
var select = document.getElementById("dropDownId");
for ( var i = 0; i < jsArr.length; i++ ){
var option = document.createElement("option");
// set the text that displays for the option
option.innerHTML = status[i];
// add the option to your select dropdown
select.appendChild(option);
}