ajax

1

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'add.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	
  <script type="text/javascript" src="js/jquery-1.10.2.js"></script>
  <script type="text/javascript">
  		$(function(){
  			$("#myfrom").submit(function(){
  				var bookName=$("input[name='bookName']").val();
  				var author=$("input[name='author']").val();
  				var bookTime=$("input[name='bookTime']").val();
  				var type=$("select[name='type']").val();
  				var reg=/^\d{4}-\d{2}-\d{2}$/;
  				if(bookName==""){
  					alert("图书名字不能为空!");
  					return false;
  				}else if(author==""){
  					alert("图书作者不能为空!");
  					return false;
  				}else if(bookTime==""){
  					alert("购买日期不能为空!");
  					return false;
  				}else if(type=="0"){
  					alert("请选择图书类别!");
  					return false;
  				}else if(!reg.test(bookTime)){
  					alert("购买日期格式不正确!");
  					return false;
  				}
  				return true;
  			});
  			
  		});
  
  </script>
  </head>
  
  <body>
  		<form action="BookServlet" method="post" id="myfrom">
    	<table width="500" cellspacing="0" cellpadding="0" align="center" border="0">
    		<tr>
    			<td></td>
    			<td><h3>新增图书信息</h3></td>
    			<td></td>
    		</tr>
    		<tr>
    			<td>图书名称:</td>
    			<td><input type="text" name="bookName"/></td>
    			<td></td>
    		</tr>
    		<tr>
    			<td>图书作者:</td>
    			<td><input type="text" name="author"/></td>
    			<td></td>
    		</tr>
    		<tr>
    			<td>购买日期:</td>
    			<td><input type="text" name="bookTime"/></td>
    			<td>yyyy-MM-dd格式</td>
    		</tr>
    		<tr>
    			<td>图书类别:</td>
    			<td>
    				<select name="type">
    					<option value="0">选择所属分类</option>
    					<option value="1">计算机/软件</option>
    					<option value="2">小说/文摘</option>
    					<option value="3">杂项</option>
    				</select>
    			</td>
    			<td></td>
    		</tr>
    		<tr>
    			<td></td>
    			<td><input type="submit" value="增加图书"/></td>
    			<td></td>
    		</tr>
    	</table>
    	</form>
  </body>
</html>

2

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
 	<style type="text/css"></style>
  <script type="text/javascript" src="js/jquery-1.10.2.js"></script>
  <script type="text/javascript">
  		$(function(){
  			
  			$("tr:first").css("background-color","pink");
  			$("tr:not(tr:first):odd").css("background-color","gray");
  			
  			
  			$("#myfrom").submit(function(){
  				/* var bookName=$("input[name='bookName']").val();
  				var author=$("input[name='author']").val();
  				var bookTime=$("input[name='bookTime']").val();
  				var type=$("select[name='type']").val();
  				$.post("BookServlet?opr=add",{bookName:bookName,author:author,bookTime:bookTime,type:type},function(){},"JSON"); */
  				//var paramsArray= $(this).serializeArray();
  				//alert(paramsArray);
  				//var params= $.param(paramsArray);
  				//alert(params);
  				//如果使用ajax的方式做表单提交数据(查询,新增,修改) 基于form表单获取数据
  				var params=$(this).serialize();
  				$.post("BookServlet?opr=add",params,function(data){
  					alert("data==>"+data);
  					if(data!=null){
  						alert("新增成功!");
  						//新建一行数据,添加到表格中
  						var newTr= $("<tr><td>"+data.bookName+"</td><td>"+data.author+"</td><td>"+data.bookTime+"</td><td>"+data.type+"</td><td><a href='javascript:void(0);' οnclick='del(this,"+data.bookId+")'>删除</a></td></tr>");
  						$("#table").append(newTr);
  						$("tr").css("background-color","white");
  						$("tr:first").css("background-color","pink");
  						$("tr:not(tr:first):odd").css("background-color","gray");
  					}else{
  						alert("新增失败!");
  					}
  				},"JSON"); 
  			
  			});
  			
  		});
  		
  		function del(obj,id){
  		
  			if(confirm("确定删除么?")){
  				$.post("BookServlet?opr=del",{bookId:id},function(data){
  					if(data==true){
  						alert("删除成功!");
  						$(obj).parent().parent().remove();
  						$("tr").css("background-color","white");
  						$("tr:first").css("background-color","pink");
  						$("tr:not(tr:first):odd").css("background-color","gray");
  					}else{
  						alert("删除失败!");
  					} 
  				},"JSON");
  			}
  		}
  
  </script>
  </head>
  
  <body>
    	<table id="table" width="700" cellspacing="0" cellpadding="0" align="center" border="1">
    		<caption>
    			<h1>图书信息</h1>
    		</caption>
    		<tr>
    			<th>图书名称</th>
    			<th>图书作者</th>
    			<th>购买时间</th>
    			<th>图书分类</th>
    			<th>操作</th>
    		</tr>
    		<c:forEach items="${list }" var="li">
    			<tr>
    				<td>${li.bookName }</td>
    				<td>${li.author }</td>
    				<td>${li.bookTime }</td>
    				<td>${li.type }</td>
    				<td>
    					<a href="javascript:void(0);" onclick="del(this,${li.bookId})">删除</a>
    				</td>
    			</tr>
    		</c:forEach>
    		
    	</table>
    	
    	<form action="javascript:void(0);" method="post" id="myfrom">
    	<table width="500" cellspacing="0" cellpadding="0" align="center" border="0">
    		<tr>
    			<td></td>
    			<td><h3>新增图书信息</h3></td>
    			<td></td>
    		</tr>
    		<tr>
    			<td>图书名称:</td>
    			<td><input type="text" name="bookName"/></td>
    			<td></td>
    		</tr>
    		<tr>
    			<td>图书作者:</td>
    			<td><input type="text" name="author"/></td>
    			<td></td>
    		</tr>
    		<tr>
    			<td>购买日期:</td>
    			<td><input type="text" name="bookTime"/></td>
    			<td>yyyy-MM-dd格式</td>
    		</tr>
    		<tr>
    			<td>图书类别:</td>
    			<td>
    				<select name="type">
    					<option value="0">选择所属分类</option>
    					<option value="1">计算机/软件</option>
    					<option value="2">小说/文摘</option>
    					<option value="3">杂项</option>
    				</select>
    			</td>
    			<td></td>
    		</tr>
    		<tr>
    			<td></td>
    			<td><input type="submit" value="增加图书"/></td>
    			<td></td>
    		</tr>
    	</table>
    	</form>
    	
    	
  </body>
</html>

3

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	
	
	
  <script type="text/javascript" src="js/jquery-1.10.2.js"></script>
  <script type="text/javascript">
  		$(function(){
  			/* $.post("TopicServlet",null,function(data){
  				//alert(data);
  				var str="";
  				$(data).each(function(){
  					str+="<li>&#160;&#160;&#160;&#160; "+this.tname+" &#160;&#160;&#160;&#160; <a href='topic_modify.jsp?tid="+this.tid+"'>修改</a>&#160;&#160;&#160;&#160; <a href='../TranServlet?opr=del&tid="+this.tid+"' οnclick='return confirm(\"确定删除么?\")'>删除</a></li>";
  				});
  				$("#myul").append(str);
  			},"JSON"); */
  			
  			$.getJSON("TopicServlet",null,function(data){
  				var str="";
  				$(data).each(function(){
  					str+="<li>&#160;&#160;&#160;&#160; "+this.tname+" &#160;&#160;&#160;&#160; <a href='topic_modify.jsp?tid="+this.tid+"'>修改</a>&#160;&#160;&#160;&#160; <a href='../TranServlet?opr=del&tid="+this.tid+"' οnclick='return confirm(\"确定删除么?\")'>删除</a></li>";
  				});
  				$("#myul").append(str);
  			});
  		});
  
  </script>
  </head>
  
  <body>
    	<ul id="myul">
    		
    	</ul>
  </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值