如何在单个jsp页面中访问javascript函数中的jsp数组?
String[] abc[]={"saab","volvo","bmw","Benz","Porsche","Jaguar"};
abc[0]="saab";
如何在单个jsp页面中访问javascript函数中的jsp数组?
String[] abc[]={"saab","volvo","bmw","Benz","Porsche","Jaguar"};
abc[0]="saab";
您应该首先使用例如 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);
打印它以逐字响应并通过javascript处理它。
幕后:jsp-inline 代码在服务器端运行。javasrcript 代码在浏览器上运行..
<%
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);
}