想通过href链接传递文本框的值,该怎么得到文本框的值

新手刚学习jsp,想写个购物车功能,想问问href传值中想传文本框的值,其中值通过js随时改变了,该怎样把里面个数传到servlet里
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" 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>

你在increase和decrease函数里面加一个修改加入购物车链接的步骤,获取链接href,判断是否有num,如果没有,链接后面加上&num=$text.val(),如果有,更新num的值。这样后台就可以来获取num参数的值了。

我把你的代码修改了一下,你可以直接粘贴一下测试。

 <title>商品信息列表</title>
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript">
      function increase(id){
        var text=$("#"+id+"");
        var num=text.val();
        text.val(++num);
      }
      function decrease(id){
        var text=$("#"+id+"");
        var num=text.val();
        if(num<=1){
          return;
        }
        text.val(--num);
      }

      function submit(id){
          var condition="num="+$("#"+id+"").val();
          window.location.assign("shoppingcarServlet?id="+id+"&"+condition);
      }
</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(${goodslist.id});">
                        <input size="1" maxlength="4" type="text" id="${goodslist.id}" name="num" value="1"/>
                        <input type="button" value="+" onclick="increase(${goodslist.id});"/></td>
                    <td><a href="javascript:submit(${goodslist.id})">加入购物车</a></td>
                </tr>
           </c:forEach>
</table>
</body>
</html>