从一个表单中获取值并在另一个表单

时间:2016-12-21 17:09:17

标签: javascript java forms jsp

我想从我的jsp文件中的2个表单中的一个中检索2个输入字段值,并在我的第二个表单中使用该值。所以我使用javascript从第一个表单中获取值并将其显示在第二个表单中的隐藏输入字段中,以便我可以使用这些值,但它给了我一个数字格式异常,所以我猜测来自第一个表单未正确传递给第二个表单。这是我的代码:

jsp文件:

<script>
             function getNumOfDays(){
                 var numOfDays = document.getElementById("numOfDays").value;
                 document.getElementById("hiddenNumDays").value = numOfDays;
             }

             function getTotalMiles(){
                 var totalMiles = document.getElementById("totalMile").value;
                 document.getElementById("hiddenTotalMiles").value = totalMiles;
             }

             function getTotalDue(){
                 var HtotalDue = document.getElementById("totalDue").value;
                 document.getElementById("hiddentTotalDue").value = HtotalDue;
             }
        </script>

第一表格

<form class="form-horizontal" name="selectVehicleForm" action="SelectVehicle">
              <div class="form-group">
                    <label class="col-md-5 control-label">Number of Days Renting:</label>
                    <div class="col-md-3">
                        <input class="form-control" type="text" id="numOfDays" name="numOfDays" disabled>                   
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-5 control-label">Total Miles:</label>
                    <div class="col-md-3">
                        <input class="form-control" type="text" name="totalMile" id="totalMile" placeholder="approximate">                  
                    </div>
                </div>



                <div class="form-group">
                    <label class="col-md-5 control-label">Total Amount Due: $</label>
                    <div class="col-md-3">
                        <input class="form-control" type="text" name="totalDue" id="totalDue" disabled>                  
                    </div>
                </div> 
            </form>

第二表格

 <input type="text" name="hiddenNumDays" id="hiddenNumDays" disabled>
               <input type="hidden" name="hiddenTotalMiles" id="hiddenTotalMiles" disabled>
               <input type="hidden" name="hiddentTotalDue" id="hiddentTotalDue" disabled>

的Serlvet:

@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        Payment paymentInfo = new Payment();
        paymentInfo.setId(10);
        paymentInfo.setFirstName(request.getParameter("firstName"));
        paymentInfo.setLastName(request.getParameter("lastName"));
        paymentInfo.setAddress(request.getParameter("address"));
        paymentInfo.setAptSuit(request.getParameter("aptNum"));
        paymentInfo.setCity(request.getParameter("city"));
        paymentInfo.setState(request.getParameter("state"));
        paymentInfo.setZipcode(request.getParameter("zipCode"));
        paymentInfo.setPhoneNum(request.getParameter("phoneNum"));
        paymentInfo.setEmail(request.getParameter("email"));
        paymentInfo.setCreditCardType(request.getParameter("card"));
        paymentInfo.setCreditCardNum(Integer.parseInt(request.getParameter("cardNumber")));
        paymentInfo.setExpirationDate(request.getParameter("expMonth"));
        paymentInfo.setCvCode(Integer.parseInt(request.getParameter("cvCode")));
        paymentInfo.setNumOfDays(Integer.parseInt(request.getParameter("hiddenNumDays")));
        paymentInfo.setTotalDue(request.getParameter("hiddenTotalDue"));

        int result = RentService.insertPaymentDB(paymentInfo);

        if(result > 0)
        {
            int vehicleID = Integer.parseInt(request.getParameter("id"));
            InventoryService.updateVehicleAvailabilty(vehicleID);
            response.sendRedirect("RentCar");
        }
        else
        {
            response.sendRedirect("failure.jsp");
        }
    }

1 个答案:

答案 0 :(得分:1)

为什么不把你从这两种形式中的任何一种中获取的价值放在这样的会话中

request.getsession.setAttribute("name", value);

然后你可以访问它

String value=session.getAttribute("name").toString();