当前位置: 移动技术网 > IT编程>脚本编程>Ajax > ajax实现用户名校验的传统和jquery的$.post方式(实例讲解)

ajax实现用户名校验的传统和jquery的$.post方式(实例讲解)

2017年12月07日  | 移动技术网IT编程  | 我要评论
第一种:传统的ajax异步请求,后台代码以及效果在最下边 首先我们在eclipse中创建一个注册页面regist.jsp,创建一个form表单,注意,由于我们只是实现用户

第一种:传统的ajax异步请求,后台代码以及效果在最下边

首先我们在eclipse中创建一个注册页面regist.jsp,创建一个form表单,注意,由于我们只是实现用户名校验的效果,下边红色部门是我们需要研究对象,所以其他的部门可以忽略不看。

内容如下:

<%@ page language="java" contenttype="text/html; charset=utf-8"
pageencoding="utf-8"%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>用户注册</title>
<link rel="stylesheet" type="text/css" href="${pagecontext.request.contextpath }/css/login.css" rel="external nofollow" >
<script type="text/javascript">
//第三步:ajax异步请求用户名是否存在
 function checkusername(){
// 获得文本框值:
var username = document.getelementbyid("username").value;
// 1.创建异步交互对象
var xhr = createxmlhttp();//第二步中已经创建xmlhttprequest,这里直接调用函数就可以了。
// 2.设置监听
xhr.onreadystatechange = function(){
if(xhr.readystate == 4){
if(xhr.status == 200){

//把返回的数据放入到span中
document.getelementbyid("span").innerhtml = xhr.responsetext;//responsetext是后台返回的数据
}
}
}
// 3.打开连接
xhr.open("get","${pagecontext.request.contextpath}/user_findbyname.action?time="+new date().gettime()+"&username="+username,true);
// 4.发送
xhr.send(null);
} 


//第二部:创建xmlhttp对象
function createxmlhttp(){
var xmlhttprequest;
try{ // firefox, opera 8.0+, safari
xmlhttp=new xmlhttprequest();
}
catch (e){
try{// internet explorer
xmlhttp=new activexobject("msxml2.xmlhttp");
}
catch (e){
try{
xmlhttp=new activexobject("microsoft.xmlhttp");
}
catch (e){}
}
}
return xmlhttprequest;
} 
function change(){
var img1 = document.getelementbyid("checkimg");
img1.src="${pagecontext.request.contextpath}/checkimg.action?"+new date().gettime();
}
</script>
</head>
<body>
<form action="${pagecontext.request.contextpath }/user_regist.action" method="post" onsubmit="return checkform()";>
<div class="regist">
<div class="regist_center">
<div class="regist_top">
<div class="left fl">会员注册</div>
<div class="right fr"><a href="${pagecontext.request.contextpath }/index.jsp" rel="external nofollow" target="_self">小米商城</a></div>
<div class="clear"></div>
<div class="xian center"></div>
</div>
<div class="regist_main center">

//第一步:首先,我们创建一个用户名input输入框,并添加一个onblur="checkusername()"事件
<div class="username">用  户  名:  <input class="shurukuang" type="text" id="username" name="username" onblur="checkusername()"/><span id="span"></span></div>
<div class="username">密        码:  <input class="shurukuang" type="password" id="password" name="password"/></div>	
<div class="username">确认 密码: <input class="shurukuang" type="password" id="repassword" name="repassword" /></div>
<div class="username">邮  箱  号:  <input class="shurukuang" type="email" id="email" name="email" /></div>
<div class="username">姓        名:  <input class="shurukuang" type="text" id="name" name="name"/></div>
<div class="username">手  机  号:  <input class="shurukuang" type="text" id="phone" name="phone"/></div>
<div class="username">地        址:  <input class="shurukuang" type="text" id="addr" name="addr"/></div>
<div class="username">
<div class="left fl">验  证  码:  <input class="yanzhengma" type="text" id="checkcode" name="checkcode" maxlength="4"/></div>
<div class="right fl"><img id="checkimg" class="captchaimage" src="${pagecontext.request.contextpath}/checkimg.action" onclick="change()" title="点击更换验证码"></div>
<div class="clear"></div>
</div>
</div>
<div class="regist_submit">
<input class="submit" type="submit" name="submit" value="立即注册" >
</div>	
</div>
</div>
</form>
</body>
</html>

第二种方式:使用jquery中的ajax实现以上效果。首先form表单以及action中的都不变,我们只需改变script就可以了。

第一步:引入js文件<script type="text/javascript" src="${pagecontext.request.contextpath }/js/jquery-3.2.1.min.js"></script>

第二步:

//ajax异步请求用户名是否存在
$(function(){
$('#username').change(function(){//给username添加一个change事件
var val = $(this).val();//获取输入框的值
val = $.trim(val);//去空
if(val != ""){//判断值是否为空
var url = "${pagecontext.request.contextpath}/user_findbyname.action";//url还是那个url
var args ={"time":new date().gettime(),"username":val};//这里和上面不同的是,这里用json方式实现传入的time和username参数
$.post(url,args,function(data){//发送post请求,后台返回的数据在data里面,
$('#span').html(data);//把后台返回的数据放入span中
});
}	
});
})

然后我们来看一下后台数据上会怎么返回的。由于我这是使用ssh框架实现的,为了方便,所以我只展示在action中是怎么返回数据的,关于ssh框架中service层,dao层的实现请自行解决。

public class useraction extends actionsupport implements modeldriven<user> {
private static final long serialversionuid = 1l;
/**
* 模型驱动
*/
private user user = new user();

 

@override
public user getmodel() {

 

return user;
}

// 注入userservice
private userservice userservice;

public void setuserservice(userservice userservice) {
this.userservice = userservice;
}
/**
* ajax进行异步校验用户名的执行方法
* 
* @throws ioexception
*/
public string findbyname() throws ioexception {
user existuser = userservice.findbyname(user.getusername());//调用service层的方法返回数据库中查询出来的对象
// 获得response对象,向页面输出:
httpservletresponse response = servletactioncontext.getresponse();
response.setcontenttype("text/html;charset=utf-8");//设置编码格式
// 判断返回的对象是否为空
if (existuser != null) {
// 如果有,查询到该用户:用户名已经存在
response.getwriter().println("用户名已经存在");
} else {
// 如果没有,用户名可以使用
response.getwriter().println("<font color='green'>用户名可以使用</font>");
}
return none;//此处返回空
}

效果如下:

以上这篇ajax实现用户名校验的传统和jquery的$.post方式(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网