为什么我的这段代码只有第一行显示点击后的结果,点击其他行的+ 或者 - 数量和价格也都在第一行变化
还有用循环从数据库得到的gprice(商品价格) 当 选择多个商品加入购物车时,价格都变成最后选中的那个商品的价格?为什么没有得到各自的价格?
这是购物车的代码
上张效果图,点击第二行效果也都显示在第一行
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="Db.Db"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.Date"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%
request.setCharacterEncoding("utf-8");
String uaddr = null;
int uid = 0;
String uname = request.getParameter("uname");
String[] strgid = request.getParameterValues("addOrders");
Connection conn = null;
Statement stmt = null;
Statement stmt1 = null;
ResultSet rs = null;
ResultSet rs1 = null;
conn = Db.getconn();
stmt = conn.createStatement();
stmt1 = conn.createStatement();
System.out.println(uname);
rs1 = stmt1.executeQuery("select * from user where uname = '"+ uname + "' ");
if (rs1.next()) {
uaddr = rs1.getString("uaddr");
uid = rs1.getInt("uid");
}
%>
<%
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowTime = sdf.format(new Date());
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>购物车</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form method="post" action="servlet/AddOrdersServlet" name="numSender">
<input type="hidden" name="uid" value="<%=uid%>">
<input type="hidden" name="uname" value="<%=uname%>">
<input type="hidden" name="oaddr" value="<%=uaddr%>">
<input type="hidden" name="dealtime" value="<%=nowTime%>">
<table border=1 align="center">
<tr>
<td colspan="4" align="center">购物车</td>
</tr>
<tr>
<td align="center">商品</td>
<td align="center">数量</td>
<td align="center" colspan="1">操作</td>
</tr>
<%
for (int i = 0; i < strgid.length; i++) {
int gid = Integer.parseInt(strgid[i]);
rs = stmt.executeQuery("select * from goods where gid =" + gid);
if (rs.next()) {
String strgprice = rs.getString("gprice");
Double p =Double.parseDouble(strgprice);
// System.out.println("p="+p);
%>
<script type="text/javascript">
function jianC() {
var p1 = <%=p%>;
/*定义变量K,用商品数量输入框里面的数字赋值*/
var k = document.getElementById("shuru").value;
/*点击-号按钮会递减商品数量*/
document.getElementById("shuru").value--;
/*点击-号按钮会递减商品价格*/
document.getElementById("cost").value = k * p1 - p1 + "元"
if (k <= 1) {
alert("亲,商品数量不能小于1!");
shuru.value = 1;
}
if (k <= 99)
p1.innerHTML = "";
}
function adddC() {
var p1 = <%=p%>;
/*点击+号按钮会递加商品数量*/
var f = document.getElementById("shuru").value;
document.getElementById("shuru").value++;
/*点击+号按钮会递加商品价格*/
document.getElementById("cost").value = f * p1 + p1 + "元";
if (f >= 99) {
alert("您所选择的数量已经到达库存上限!");
shuru.value = 99;
p1.style.color = "red";
p1.innerHTML = "<p><strong>所选数量已经到达上限!</strong></p>";
} else {
document.getElementById("shuru").value + 1;
}
}
function ok()
{
document.getElementById("district").value=document.numSender.init_num.value ;
}
</script>
<!-- ss -->
<tr>
<td><img src="<%=rs.getString("gpicture")%>"> <%=rs.getString("gname")%></td>
<td><input type="button" id="jian" onClick="jianC()" value="-">
<input id="shuru" name="init_num" class="input_text" type="text" value="0" size="3">
<input type="hidden" id="district" name="district" value="" >
<input type="button" id="addd" onClick="adddC()" value="+"></td>
<td>
<input type="checkbox" name="gid" value="<%=gid %>">
</td>
</tr>
<%
}
}
rs.close();
rs1.close();
stmt.close();
stmt1.close();
conn.close();
%>
<tr>
<td colspan="4" align="center">
总价:<input id="cost" class="input_text" type="text" value="0"
size="3" value="0"></td>
</tr>
<tr>
<td colspan="5" align="center">
<button id="buy_anniu" type="submit">提交</button>
<button type="reset">重置</button>
</td>
</tr>
</table>
</form>
</body>
</html>