jQuery实例--Ajax信息的增删改查等操作

<html>
	<head>
		<title>jQuery实例</title>
		<style>
			
		</style>
		<script type="text/javascript" src="jquery-1.8.2.js"></script>
		<script type="text/javascript">
			$(function(){
				//调用加载数据函数
				loadstu();
				
				//为添加按钮绑定一个添加动作
				$("#aid").click(function(){
					$("#fill").show();
					$("#myform").show();
				});
			});
			
			//加载学生信息
			function loadstu(){
				$.ajax({
					url:"./model/action.php?a=index",
					type:"GET",
					dataType:"json",
					success:function(data){
						//alert(data);
						var str=null;
						for(var i=0;i<data.length;i++){
							str+="<tr>";
							str+="<td>"+data[i].id+"</td>";
							str+="<td>"+data[i].name+"</td>";
							str+="<td>"+data[i].sex+"</td>";
							str+="<td>"+data[i].age+"</td>";
							str+="<td>"+data[i].classid+"</td>";
							str+="<td><a href=\"javascript:void(0);\" οnclick='doDel("+data[i].id+",this)'>删除</a></td>";
							str+="</tr>";
							
						}
						$("#stuid tbody").empty();
						$("#stuid tbody").append(str);
					}
				});
			}
			
			//删除函数
			function doDel(sid,ob){
				//执行ajax删除
				if(confirm("确定要删除吗?"))
				$.ajax({
					url:"./model/action.php?a=del",
					type:"POST",
					data:{id:sid},
					dataType:"json",
					success:function(res){
						//判断是否删除成功
						if(res.num>0){
							//alert(res.num);
							$(ob).parents("tr").remove();
						}
					},
					error:function(){
						alert('error');
					}
				});
			}
			
			//执行添加
			function doAdd(form){
				//alert($(form).serialize());
				$.ajax({
					url:"./model/action.php?a=insert",
					type:"POST",
					data:$(form).serialize(),
					dataType:"json",
					success:function(res){
						if(res.id>0){
							$('#fill').hide();
							$('#myform').hide();
							$(form).find(":reset").trigger("click");
							loadstu();//重新加载
						}else{
							alert('添加失败!');
						}
					}
				});
				
				return false;
			}
		</script>	
	</head>
	<body>
		<h2>jQuery实例--Ajax信息的增删改查等操作</h2>
		<h3>学生信息</h3><button id="aid">添加</button>
		<table id="stuid" width="500" border="1">
			<thead>
				<tr>
					<th>学号</th><th>姓名</th><th>性别</th><th>年龄</th><th>班级号</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody>
				
			</tbody>
		</table>
		
		
		<div id="fill" style="width:100%;height:100%;background-color:#000;position:absolute;top:0px;left:0px;opacity:0.3;display:none;z-index:100">
		</div>
		
		<div id="myform" style="width:100%;height:100%;position:absolute;top:0px;left:0px;z-index:101;display:none">
			<form  action="" method="post" οnsubmit="return doAdd(this)">
			<table width="340" border="0" style="margin:20% auto;background-color:#fff">
				<tr>
					<td>姓名:</td>
					<td><input type='text' name="name"/></td>
				</tr>
				<tr>
					<td>年龄:</td>
					<td><input type='text' name="age"/></td>
				</tr>
				<tr>
					<td>性别:</td>
					<td>
						<input type='radio' name="sex" value="m"/>男
						<input type='radio' name="sex" value="w"/>女
					</td>
				</tr>
				<tr>
					<td>班级:</td>
					<td><input type='text' name="classid"/></td>
				</tr>
				<tr>
					<td> </td>
					<td >
						<input type='submit' value="添加"/> 
						<input type='reset' value="重置"/> 
						<input type="button" οnclick="$('#fill').hide();$('#myform').hide();" value="关闭"/>
					</td>
				</tr>
			</table>
			</form>
		</div>
		
		
	</body>
</html>



  • 6
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值