jsp给JS数组赋值问题

mcdido 2009-06-18 02:44:21
代码如下,数据能从数据库正确读取,问题是JSP不能向JS的数组正确添加数据,望高手指点迷津。
function findprod(locationid){
var subcat = new Array();
var id=locationid;
xml = new ActiveXObject("Microsoft.XMLHTTP");
xml.open("POST","mytest.jsp?i="+id+"",false);
xml.send();
<%
int count=0;
String tid=request.getParameter("i");
Set select=new HashSet();
String p_brand="";
String hqla="select distinct PBrand from DiyProd where t_id='"+tid+"'";
if(tid!=null){
List prods=hn.query(hqla);
Iterator it=prods.iterator();
while(it.hasNext()){
p_brand=(String)it.next();
System.out.println(count);
System.out.println(p_brand+"---");
%>
subcat[<%=count%>]='<%=p_brand%>';
<%
count++;
}
}
%>
}
...全文
903 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZiSheng 2009-06-18
  • 打赏
  • 举报
回复
我的能成功,你的不能??
ZiSheng 2009-06-18
  • 打赏
  • 举报
回复
alert( <%=count%>);
改为alert("<%=count%>");
mcdido 2009-06-18
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ZiSheng 的回复:]
给你个简单的例子。
<%
int count=1;
%>
<html>
<head>
<script>
function getJSPValue(){
alert( <%=count%>);
}
</script>
</head>
<body>
<input type="button" onclick="getJSPValue();" value="点击">
</body>
</html>
数组应该也会了吧
[/Quote]
能否告知我的代码为什么会不成功?
ZiSheng 2009-06-18
  • 打赏
  • 举报
回复
给你个简单的例子。
<%
int count=1;
%>
<html>
<head>
<script>
function getJSPValue(){
alert(<%=count%>);
}
</script>
</head>
<body>
<input type="button" onclick="getJSPValue();" value="点击">
</body>
</html>
数组应该也会了吧
mcdido 2009-06-18
  • 打赏
  • 举报
回复
anybody?
mcdido 2009-06-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 windy731 的回复:]
还有一点,在subcat[ <%=count%>],需要转换一下类型


Java code
function findprod(locationid){
var id=locationid;
xml = new ActiveXObject("Microsoft.XMLHTTP");
xml.open("POST","mytest.jsp?i="+id+"",false);
xml.send();
<%
int count=0;
String tid=request.getParameter("i");
Set select=new HashSet();
String …
[/Quote]
谢谢你的热情解答,问题没能解决。。。SO CONFUSE。。
windy731 2009-06-18
  • 打赏
  • 举报
回复
还有一点,在subcat[ <%=count%>],需要转换一下类型


function findprod(locationid){
var id=locationid;
xml = new ActiveXObject("Microsoft.XMLHTTP");
xml.open("POST","mytest.jsp?i="+id+"",false);
xml.send();
<%
int count=0;
String tid=request.getParameter("i");
Set select=new HashSet();
String p_brand="";
String hqla="select distinct PBrand from DiyProd where t_id='"+tid+"'";
if(tid!=null){
List prods=hn.query(hqla);
%>
var subcat = new Array(<%=prods.size()%>); // 声明js数组时要给出数组的大小
<%
Iterator it=prods.iterator();
while(it.hasNext()){
p_brand=(String)it.next();
System.out.println(count);
System.out.println(p_brand+"---");
%>
subcat[ parseInt(<%=count%>)]=' <%=p_brand%>'; // // 这里需要转换一下类型
<%
count++;
}
}
%>
}
runshine 2009-06-18
  • 打赏
  • 举报
回复
运行页面,在浏览器里查看页面源代码
看看subcat数组定义的情况
windy731 2009-06-18
  • 打赏
  • 举报
回复

function findprod(locationid){
var id=locationid;
xml = new ActiveXObject("Microsoft.XMLHTTP");
xml.open("POST","mytest.jsp?i="+id+"",false);
xml.send();
<%
int count=0;
String tid=request.getParameter("i");
Set select=new HashSet();
String p_brand="";
String hqla="select distinct PBrand from DiyProd where t_id='"+tid+"'";
if(tid!=null){
List prods=hn.query(hqla);
%>
var subcat = new Array(<%=prods.size()%>); [color=#FF0000]// 声明js数组时要给出数组的大小[/color]<%
Iterator it=prods.iterator();
while(it.hasNext()){
p_brand=(String)it.next();
System.out.println(count);
System.out.println(p_brand+"---");
%>
subcat[ <%=count%>]=' <%=p_brand%>';
<%
count++;
}
}
%>
}
mcdido 2009-06-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhong19860904 的回复:]
...
[/Quote]
有话直说。。
mcdido 2009-06-18
  • 打赏
  • 举报
回复
补充一下,
System.out.println(count);
System.out.println(p_brand+"---");
这两行的代码是有值。
但检查JS的数组元素时,用alert发现里面并没有值,而是undefined

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧