Buy.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>购物车购买</title>
<style>
body{background-color:#0F3}
</style>
</head>

<body>
<form action="buy.jsp" method="post">
请选择:<br>
<input type="checkbox" value="1" name="book">语文书
<input type="text"  name="t1" style="width:1cm">本<br>
<input type="checkbox" value="2" name="book">数学书
<input type="text"  name="t2" style="width:1cm">本<br>
<input type="checkbox" value="3" name="book">英语书
<input type="text"  name="t3" style="width:1cm">本<br>
<input type="checkbox" value="4" name="book">物理书
<input type="text"  name="t4" style="width:1cm">本<br>
<input type="checkbox" value="5" name="book">化学书
<input type="text"  name="t5" style="width:1cm">本<br>
<input type="checkbox" value="6" name="book">生物书
<input type="text"  name="t6" style="width:1cm">本<br>
<input type="checkbox" value="7" name="book">地理书
<input type="text"  name="t7" style="width:1cm">本<br>
<input type="submit" value="提交">
</body>
</html>
buy.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>总价</title>
</head>
<body>
<%
   double sum=0.0;
   double [] prize={23.0,56.5,45.2,12.2,56.2,59.3,12.0};
   String [] checkbox=request.getParameterValues("book");
   for(int i=0;i<checkbox.length;i++)
   {
       String k="t"+checkbox[i];
       int num=Integer.parseInt(request.getParameter(k));
       int t=Integer.parseInt(checkbox[i]);
       sum+=prize[t]*num;
   }
   String str=Double.toString(sum);
   out.write("总共花费金额为:"+str+"元");
%>
</body>
</html>

效果:

0
Posted in Java Web

Leave a Comment:

电子邮件地址不会被公开。