index.jsp
上传用户:qingshan
上传日期:2022-03-24
资源大小:9089k
文件大小:4k
源码类别:

Ajax

开发平台:

Java

  1. <%--
  2.     Document   : index
  3.     Created on : 2010-1-11, 9:51:09
  4.     Author     : TFPC
  5. --%>
  6. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  7. <%@page import="java.util.*,Entity.Item" %>
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  9.    "http://www.w3.org/TR/html4/loose.dtd">
  10.    <script type="text/javascript" src="js/loadjs.js"></script>
  11.    <script>
  12.     function shoplist(){                //显示购物清单
  13.        document.getElementById("shoplist").style.display="";
  14.        }
  15.     function shop(){                     //继续购物
  16.        document.getElementById("shoplist").style.display="none";
  17.        }
  18.     var request=getXMLHttp();
  19.     function additem(num,price){                 //添加物品至购物单
  20.         //使用Ajax传递数据后得到用户的购买清单
  21.       if(document.getElementById(num).value){        
  22.         alert("单价为"+price);
  23.         var url="additem?itemid="+document.getElementById(num).name+
  24.           "&num="+document.getElementById(num).value+"&price="+price;
  25.         request.open("GET",url,true); 
  26.         request.onreadystatechange=function(){                   //回调函数
  27.             if(request.readyState == 4){
  28.                 if(request.status == 200){            
  29.             var text=eval('('+request.responseText+')');         //动态操作dom元素
  30.             alert("你已购买"+text.name);
  31.             var newtr=document.createElement("tr");
  32.             var td1=document.createElement("td");
  33.             td1.appendChild(document.createTextNode(text.name));
  34.             newtr.appendChild(td1);
  35.             var td2=document.createElement("td");
  36.             td2.appendChild(document.createTextNode(text.num))
  37.             newtr.appendChild(td2);
  38.             var td3=document.createElement("td");
  39.             td3.appendChild(document.createTextNode(text.price))
  40.             newtr.appendChild(td3);
  41.             var td4=document.createElement("td");
  42.             td4.appendChild(document.createTextNode(text.sum))
  43.             newtr.appendChild(td4);
  44.             document.getElementById("listtable").appendChild(newtr);
  45.                 } }
  46.       }//回调结束
  47.        request.send(null);
  48.        if(comfirm("是否查看购物车?")){
  49.            shoplist()
  50.        }
  51.    }else{
  52.          alert("请输入购买数量!");
  53.          return null;
  54.       }
  55.     }
  56.    </script>
  57. <html>
  58.     <head>
  59.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  60.         <title>JSP Page</title>
  61.     </head>
  62.     <body>
  63.         <h1 align="center">欢迎购物</h1>
  64.         <div align="center" id="shop">
  65.             <input type="button"  value="查看购物车" onclick="shoplist()"><br>                     
  66.           <table border="1">
  67.               <tr><th>商品名称</th><th>单价</th><th>购买数量</th><th>添加至购物车</th></tr>
  68.               <tr>
  69.               <td>面包</td><td>1</td>
  70.               <td><input type="text" name="bread" id="num1"></td>
  71.               <td>
  72.                   <input type="button" value="添加" onclick="additem('num1',1)"/>
  73.               </td>
  74.               </tr>
  75.               <tr>
  76.               <td>牛奶</td><td>4</td>
  77.               <td><input type="text" name="milk" id="num2"></td>
  78.               <td>
  79.                   <input type="button" value="添加" onclick="additem('num2',4)"/>
  80.               </td>
  81.               </tr>
  82.               <tr>
  83.               <td>香蕉</td><td>6</td>
  84.               <td><input type="text" name="banana" id="num3"></td>
  85.               <td>
  86.                   <input type="button" value="添加" onclick="additem('num3',6)"/>
  87.               </td>
  88.               </tr>
  89.           </table>
  90.         </div>
  91.         <div align="center" id="shoplist" style="display: none;">
  92.             <table id="listtable" border="1">              
  93.               你已经购买了下列产品:
  94.               <tr><th>商品名称</th><th>商品数量</th><th>商品单价</th><th>商品总价</th></tr>
  95.               
  96.           </table>
  97.           <input type="button"  value="继续购物" onclick="shop()"><br>
  98.            <input type="button"  value="付款"/>
  99.         </div>
  100.     </body>
  101. </html>