问题是购物车只有一个商品的时候就可以修改成功,两件以上点击修改按钮以后购物车的商品数总是变为modifyForm里面的mod的初始值1,请问怎么办,这个问题我至少搞了5天了,求救!!!!购物车的代码如下:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><html>
<head>
<title>购物车</title>
<link href="size.css" rel="stylesheet" type="text/css"><script language="javascript">
<!--
function modQuant()
{
document.modifyForm.mod.value=document.quantForm.quant.value;
}
//-->
</script>
</head>
<body>
<%
request.setCharacterEncoding("GB2312");
String kind=request.getParameter("kind");
%>
<c:set var="kind1" scope="page">
<%=kind%>
</c:set>
<c:choose>
<c:when test="${ !empty kind1 }" ><c:set var="buylist" value="${sessionScope.shoppingcart}" /><c:if test="${ !empty buylist }" ><br>
<center>
<font>目前您购物车的内容如下:</font><br><hr width="770"></center><table border="1" width="770" align="center">
<tr bgcolor="#C6C9E6">
<td ><div align="center"><b>书 名</b></div></td>
<td ><div align="center"><b>作 者</b></div></td>
<td ><div align="center"><b>出版社</b></div></td>
<td ><div align="center"><b>价格(人民币)</b></div></td>
<td ><div align="center"><b>数 量</b></div></td>
<td ><div align="center"><b>操 作</b></div></td>
</tr>
<c:forEach items="${buylist}" var="order" varStatus="status" >
<tr>
<td><b>${order.name}</b></td>
<td><b>${order.author}</b></td>
<td><b>${order.publisher}</b></td>
<td><b><div align="right">${order.price}</div></b></td>
<td><b>
<div align="center">
<form name="quantForm">
<input type="text" name="quant" value='${order.quantity}' size="3">
</form>
</div>
</b></td>
<td>
<form name="deleteForm" action="ShoppingServlet" method="POST">
<div align="center"><input type="submit" value="删除"></div>
<input type="hidden" name="del" value='${status.index}'>
<input type="hidden" name="kind" value='<%=kind%>' >
<input type="hidden" name="action" value="DELETE">
</form>
<form name="modifyForm" action="ShoppingServlet" method="POST">
<input type="hidden" name="mod" value=1 > <!--设默认值 -->
<div align="center"><input type="submit" value="修改" onClick="javascript:modQuant();"></div>
<input type="hidden" name="modIndex" value='${status.index}'>
<input type="hidden" name="kind" value='<%=kind%>' >
<input type="hidden" name="action" value="MODIFY">
</form>
</td>
</tr>
</c:forEach></table>
<form name="checkoutForm" action="ShoppingServlet" method="POST">
<input type="hidden" name="action" value="CHECKOUT">
<input type="hidden" name="kind" value='<%=kind%>' >
<input type="submit" name="Checkout" value="付款结账">
</form>
<a class=bookn href="showbook.jsp?kind=<%=kind%>">是否继续购物?</a>
</c:if>
</c:when>
<c:otherwise >
<br>
<center>您的购物车为空.</center><br>
<script language="javascript">
<!--
function clock(){i=i-1
document.title="本窗口将在"+i+"秒后自动关闭!";
if(i>0)setTimeout("clock();",1000);
else self.close();}
var i=3
clock();
//-->
</script>
</c:otherwise>
</c:choose></body>
</html>---------------------------------------------servlet代码如下:
//修改购物车
if(action.equals("MODIFY")){
String modIndex = req.getParameter("modIndex");
String mod=req.getParameter("mod"); //取得修改值
int d1 = (new Integer(modIndex)).intValue();
int d2= (new Integer(mod)).intValue();
System.out.println(d2);
Book modBook=(Book)buylist.elementAt(d1);
modBook.setQuantity(d2);
buylist.setElementAt(modBook,d1);
url = "/Cart.jsp?kind="+kind;
}
...
session.setAttribute("shoppingcart", buylist);
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req, res);
...
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><html>
<head>
<title>购物车</title>
<link href="size.css" rel="stylesheet" type="text/css"><script language="javascript">
<!--
function modQuant()
{
document.modifyForm.mod.value=document.quantForm.quant.value;
}
//-->
</script>
</head>
<body>
<%
request.setCharacterEncoding("GB2312");
String kind=request.getParameter("kind");
%>
<c:set var="kind1" scope="page">
<%=kind%>
</c:set>
<c:choose>
<c:when test="${ !empty kind1 }" ><c:set var="buylist" value="${sessionScope.shoppingcart}" /><c:if test="${ !empty buylist }" ><br>
<center>
<font>目前您购物车的内容如下:</font><br><hr width="770"></center><table border="1" width="770" align="center">
<tr bgcolor="#C6C9E6">
<td ><div align="center"><b>书 名</b></div></td>
<td ><div align="center"><b>作 者</b></div></td>
<td ><div align="center"><b>出版社</b></div></td>
<td ><div align="center"><b>价格(人民币)</b></div></td>
<td ><div align="center"><b>数 量</b></div></td>
<td ><div align="center"><b>操 作</b></div></td>
</tr>
<c:forEach items="${buylist}" var="order" varStatus="status" >
<tr>
<td><b>${order.name}</b></td>
<td><b>${order.author}</b></td>
<td><b>${order.publisher}</b></td>
<td><b><div align="right">${order.price}</div></b></td>
<td><b>
<div align="center">
<form name="quantForm">
<input type="text" name="quant" value='${order.quantity}' size="3">
</form>
</div>
</b></td>
<td>
<form name="deleteForm" action="ShoppingServlet" method="POST">
<div align="center"><input type="submit" value="删除"></div>
<input type="hidden" name="del" value='${status.index}'>
<input type="hidden" name="kind" value='<%=kind%>' >
<input type="hidden" name="action" value="DELETE">
</form>
<form name="modifyForm" action="ShoppingServlet" method="POST">
<input type="hidden" name="mod" value=1 > <!--设默认值 -->
<div align="center"><input type="submit" value="修改" onClick="javascript:modQuant();"></div>
<input type="hidden" name="modIndex" value='${status.index}'>
<input type="hidden" name="kind" value='<%=kind%>' >
<input type="hidden" name="action" value="MODIFY">
</form>
</td>
</tr>
</c:forEach></table>
<form name="checkoutForm" action="ShoppingServlet" method="POST">
<input type="hidden" name="action" value="CHECKOUT">
<input type="hidden" name="kind" value='<%=kind%>' >
<input type="submit" name="Checkout" value="付款结账">
</form>
<a class=bookn href="showbook.jsp?kind=<%=kind%>">是否继续购物?</a>
</c:if>
</c:when>
<c:otherwise >
<br>
<center>您的购物车为空.</center><br>
<script language="javascript">
<!--
function clock(){i=i-1
document.title="本窗口将在"+i+"秒后自动关闭!";
if(i>0)setTimeout("clock();",1000);
else self.close();}
var i=3
clock();
//-->
</script>
</c:otherwise>
</c:choose></body>
</html>---------------------------------------------servlet代码如下:
//修改购物车
if(action.equals("MODIFY")){
String modIndex = req.getParameter("modIndex");
String mod=req.getParameter("mod"); //取得修改值
int d1 = (new Integer(modIndex)).intValue();
int d2= (new Integer(mod)).intValue();
System.out.println(d2);
Book modBook=(Book)buylist.elementAt(d1);
modBook.setQuantity(d2);
buylist.setElementAt(modBook,d1);
url = "/Cart.jsp?kind="+kind;
}
...
session.setAttribute("shoppingcart", buylist);
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req, res);
...
有个循环,所以如果有多笔的话呢,你的FORM就会有多个
>>>><form name="quantForm">
>>>> <input type="text" name="quant" value='${order.quantity}' size="3">
>>>></form>
以上几行并没有对这多个的form进行区别,名字是一样的,只要对name属性进行区别,比如加个索引就可以解决(其它的form也一样。)