<%@ page language="java" import="java.util.*,db.Connshangpin,bean.prudects" pageEncoding="utf-8"%>
<%
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String sql = "select * from products where cname like '%"+name+"' order by cid desc";
ArrayList list = Connshangpin.chaxun(sql);
%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript" type="text/javascript"> function a(){window.location.href="xiangmu/zhuce.jsp";}
</script> 
</style>
</head><body ><div id="Layer3">
<%if(list.size()>0&&list!=null) {%>
<%      
if (list.size() > 0) {
int nowPageNum = 6;
int pageSize = 0;
if (list.size() % nowPageNum == 0) {
pageSize = list.size() / nowPageNum;
} else {
pageSize = list.size() / nowPageNum + 1;
}
for (int j = 1; j <= pageSize; j++) {
%>
<a href="fshopping.jsp?page=<%=j%>&name=<%=name %>">[第<%=j%>页]</a>&nbsp;
<%
}
%><%
int page1 = 0;
if (request.getParameter("page") != null) {
try{
page1 = Integer.parseInt(request.getParameter("page")) - 1;
if(page1 < 0){
page1 = 0;
}
if(page1 > pageSize){
page1 = pageSize-1;
}
}catch(Exception e){
page1 = 0;
}
}
for ( int i = (page1 * nowPageNum); i < ((page1 + 1) * nowPageNum); i++) {
if (i < list.size()) {
prudects pd = (prudects) list.get(i);
%>
<br></br><br></br>
  <table width="953" height="206" border="1" cellpadding="0" cellspacing="0"  bordercolor="#0066FF">
    <tr>
      <td colspan="3" align="center"><%=pd.getCname() %></td>
    </tr>
    <tr>
 
      <td width="216" rowspan="3"  align="center"><img src="<%=pd.getCimage() %>" /></td>
      <td width="378" align="center"><%=pd.getCmiaoshu() %></span></td>
       <script language="javascript" > 
  function b(){window.location.href="buyprudects.jsp?cid=<%=pd.getCid()    %>";//这里传到buyprudects.jsp的值总是一个相同的ID,导致购物车里无论添加那样商品都是添加那一样商品
}
</script>
      <td width="343" rowspan="3" align="center"><input type="image"  src="img/goumai.gif" <%if(session.getAttribute("OK")==null){ %>onclick="javascript:a()"<%} else{%>onclick="javascript:b()"<%} %>/></td>
    </tr>
    <tr>
      <td align="center"><span >产品原价格:<%=pd.getCprice() %></span></td>
    </tr>
    <tr>
      <td align="center">会员价格:<%=pd.getChprice() %></td>
    </tr>
  </table>
<%}
} }

%>
<%}else{ %><center>抱歉,没有找到与<font color="red"><%=name %></font>相关的产品</center><%} %>
<br></br>
<br></br>
<br></br>
<br></br> <center><jsp:include flush="true" page="bottom1.jsp"></jsp:include></center>
</div></body>
</html>

解决方案 »

  1.   

    肯定的啊,你没有传一个pd的实例进去,onclick="javascript:b()",你没有传参数进去!
    pd总是list里面最后一个
      

  2.   

    function b(cid){
    window.location.href="buyprudects.jsp?cid="+cid;
    }
    <%if(session.getAttribute("OK")==null){ %>onclick="javascript:a()"<%} else{%>onclick="javascript:b('<%=pd.getCid()%>')"<%} %>
      

  3.   

    谢谢各位 现在解决了 可是还是有一些不明白  function b(){window.location.href="buyprudects.jsp?cid=<%=pd.getCid()    %>";//这里传到buyprudects.jsp的值总是一个相同的ID,导致购物车里无论添加那样商品都是添加那一样商品
    }
    一开始写的代码为什么传的只是一个ID呢 我加在循环里了  
      

  4.   

           <script language="javascript" > 
      function b(){window.location.href="buyprudects.jsp?cid=<%=pd.getCid()    %>";//这里传到buyprudects.jsp的值总是一个相同的ID,导致购物车里无论添加那样商品都是添加那一样商品
    }
    </script>改成<script language="javascript" > 
      function b(str){
    window.location.href="buyprudects.jsp?cid="+str;
    }
    </script>
     <td width="343" rowspan="3" align="center">
    <input type="image"  src="img/goumai.gif" 
    <%if(session.getAttribute("OK")==null){ %>onclick="javascript:a()"<%} 
    else{%>onclick="javascript:b('<%=pd.getCid()%>')"<%} %>/></td>
    把你的商品id作为js传递的值传进去就可以了。另外javascript的方法写在循环外,比如<body>之前。