kjwangxun 2016-01-15 12:28 采纳率: 69.2%
浏览 3011
已采纳

购物车怎么通过jsp向servlet传递商品id和购买数量?

各位大神帮忙看看
servlet里想通过request.getParameter()来获取
form试过,不能获得点击按钮的商品id,全是相同的1
jsp:

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE HTML>
<html>
  <head>
    <title>商品信息列表</title>
    <script type="text/javascript" src="js/jquery-1.11.1.js"></script>
    <script type="text/javascript">
      function increase(btn){
        var $text=$(btn).prev();
        var num=$text.val();
        $text.val(++num);
      }
      function decrease(btn){
        var $text=$(btn).next();
        var num=$text.val();
        if(num<=1){
          return;
        }
        $text.val(--num);
      }
    </script>
  </head>
  <body>
    <h2>包子店</h2>
<h4><a href="shoppingcarServlet">查看购物车</a></h4>
<hr>
<table width="80%" border="1">
    <tr>
        <th>商品名称</th>
        <th>商品图片</th>
        <th>商品价格</th>
        <th>购买数量</th>
        <th>操作</th>
    </tr>
           <c:forEach items="${goodslist}" var="goodslist">     
                <tr align="center">
                    <td>${goodslist.name}</td>
                    <td><img src="img/${goodslist.img}" width="200" height="70"></td>
                    <td>${goodslist.price}</td>
                    <td><input type="button" value="-" onclick="decrease(this);">
                        <input size="1" maxlength="4" type="text" id="num" name="num" value="1"/>
                        <input type="button" value="+" onclick="increase(this);"/></td>
                    <td><a href="shoppingcarServlet?id=${goodslist.id}">加入购物车</a></td>
                </tr>
                </c:forEach>
</table>
  </body>
</html>

图片说明这是商品页面,商品数量我想自己控制然后发送到servlet

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE HTML>
<html>
  <head>
    <title>商品信息列表</title>
    <script type="text/javascript" src="js/jquery-1.11.1.js"></script>
    <script type="text/javascript">
      function increase(btn){
        var $text=$(btn).prev();
        var num=$text.val();
        $text.val(++num);
      }
      function decrease(btn){
        var $text=$(btn).next();
        var num=$text.val();
        if(num<=1){
          return;
        }
        $text.val(--num);
      }
    </script>
  </head>
  <body>
    <h2>包子店</h2>
<h4><a href="shoppingcarServlet">查看购物车</a></h4>
<hr>
<form action="shoppingcarServlet" method="get"> 
<table width="80%" border="1">
    <tr>
        <th>商品名称</th>
        <th>商品图片</th>
        <th>商品价格</th>
        <th>购买数量</th>
        <th>操作</th>
    </tr>
           <c:forEach items="${goodslist}" var="goodslist">     
                <tr align="center">
                    <input type="hidden" name="id" value="${goodslist.id}">
                    <td>${goodslist.name}</td>
                    <td><img src="img/${goodslist.img}" width="200" height="70"></td>
                    <td>${goodslist.price}</td>
                    <td><input type="button" value="-" onclick="decrease(this);">
                        <input size="1" maxlength="4" type="text" id="num" name="num" value="1"/>
                        <input type="button" value="+" onclick="increase(this);"/></td>
                    <td><input type="submit" value="加入购物车"></td>
                </tr>
                </c:forEach>
</table>
</form>
  </body>
</html>

这是改成的form表单提交,servlet接到的id全是第一件商品的id

  • 写回答

3条回答

  • kjwangxun 2016-01-15 14:05
    关注

    自己解决了。商品id我用href传,用ajax传购买数量到servlet

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?