Blogger Information
Blog 20
fans 2
comment 0
visits 15008
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0410作业-无刷新验证
麦小糖的博客
Original
819 people have browsed it

1、HTML代码

实例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>$.ajax()</title>
</head>
<body>
	<h2>用户登录</h2>
	<form>
		<p>用户名:<input type="text" name="name"></p>
	</form>	
</body>
</html>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
	$(':input').blur(function(){
		// console.log('通过')
		// 用法1:将回调写到$.ajax()函数中
		$.ajax({
			// 
			//   参数是对象,所以必须用{}包裹
			//  1.url,请求的服务器资源
			 url:'api/demo.php',

			 // 2.客户端的请求类型:post,get,put……
			 type:'GET',

			 // 3.从服务器返回的数据格式:json,html,txt,xml
			 // dataType:'json',
			 // 4.异步(true)或同步(false,也叫服务器锁定)
			 async:true,
			 // 5.发送的数据
			 // 查询字符串方式
			 data:'name='+$(':input').val(),
			 // 6.成功回调函数,success:function(mag,status,xhr){}
			 success: function(msg,status, xhr){
			 	// console.log(msg)
			 	$('p span').empty()
			 	$('p').append($(msg))
			 }

			 // 7.错误回调函数,error:function(){xhr,status,error}
			 
		})
	})
</script>

运行实例 »

点击 "运行实例" 按钮查看在线实例

2、PHP代码

实例

<?php
// print_r($_GET);exit;

$nameList = ['admin','peter','php'];

$userName = $_GET['name'];

if(strlen(trim($userName)) == 0){
	echo '<span style="color:red">用户名不能为空</span>';
}else if (is_numeric($userName)) {
	echo '<span style="color:red">用户名不能为纯数字</span>';
}else if(in_array($userName,$nameList)){
	echo '<span style="color:red">用户名已存在</span>';
}else{
	echo '<span style="color:green">用户名可用</span>';
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

3、手写代码

0410作业.png

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post