不好意思,小弟手上目前没分,谢谢问题是这样,整个代码是可以运行的,页面也可以显示"验证通过...",但是在显示"验证通过..."之前几秒钟是空白的,如果正确的话应该是先出现"正在验证中………………",之后出现"验证通过..."下面是源码
action code...public class UserActionAjax extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
response.setCharacterEncoding("utf-8");
Thread.sleep(2000); //模拟等特时间
response.getWriter().println("验证通过...");
System.out.println("UserActionAjax");
return super.execute(mapping, form, request, response);
}HTML code ...<%@ page language="java" 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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<title>user register</title>
<script type="text/javascript">
function $(id){
return document.getElementById(id);
}
var xmlhttp;
function createHttpObject(){
try{
xmlhttp=new ActiveXObject('Msxml2.XMLHTTP');
}catch(e){
try{
xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
}catch(e){
try{
xmlhttp=new XMLHttpRequest();
}catch(e){}
}
}
return xmlhttp;
} function checkUsername(){
document.getElementById("userInfo").innerHTML="正在验证中………………";
var xmlhttp = createHttpObject();
if(xmlhttp){
var url = "/user.do?username=" + $("username").value;
xmlhttp.open("POST",url, false);
xmlhttp.onreadystatechange = complete;
xmlhttp.send(null);
}
} function complete(){
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
//alert(xmlhttp.responseText);
$("userInfo").innerHTML=xmlhttp.responseText;
}
}
}
</script>
</head> <body>
<form action="">
<table>
<tr>
<td>用户名:</td>
<td><input id="username" name="username" type="text" onblur="checkUsername()"></td>
<td><label id="userInfo"></label></td>
</tr>
<tr>
<td colspan="3">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
action code...public class UserActionAjax extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
response.setCharacterEncoding("utf-8");
Thread.sleep(2000); //模拟等特时间
response.getWriter().println("验证通过...");
System.out.println("UserActionAjax");
return super.execute(mapping, form, request, response);
}HTML code ...<%@ page language="java" 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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<title>user register</title>
<script type="text/javascript">
function $(id){
return document.getElementById(id);
}
var xmlhttp;
function createHttpObject(){
try{
xmlhttp=new ActiveXObject('Msxml2.XMLHTTP');
}catch(e){
try{
xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
}catch(e){
try{
xmlhttp=new XMLHttpRequest();
}catch(e){}
}
}
return xmlhttp;
} function checkUsername(){
document.getElementById("userInfo").innerHTML="正在验证中………………";
var xmlhttp = createHttpObject();
if(xmlhttp){
var url = "/user.do?username=" + $("username").value;
xmlhttp.open("POST",url, false);
xmlhttp.onreadystatechange = complete;
xmlhttp.send(null);
}
} function complete(){
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
//alert(xmlhttp.responseText);
$("userInfo").innerHTML=xmlhttp.responseText;
}
}
}
</script>
</head> <body>
<form action="">
<table>
<tr>
<td>用户名:</td>
<td><input id="username" name="username" type="text" onblur="checkUsername()"></td>
<td><label id="userInfo"></label></td>
</tr>
<tr>
<td colspan="3">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
你可以在后面搞个按钮什么的,或者换成其它的试试。
是你填完直接点提交的原因吧
你用的是onblur,你填完数据点下空白处 会出现"正在验证中………………"
document.getElementById("userInfo").innerHTML="正在验证中………………";
这一行在checkUsername()中去掉,
用
<td><input id="username" name="username" type="text"
onblur="document.getElementById('userInfo').innerHTML='正在验证中………………';checkUsername()">
</td>
试试
var xmlhttp = createHttpObject();
if(xmlhttp){
var url = "/user.do?username=" + $("username").value;
xmlhttp.open("POST",url, false);
xmlhttp.onreadystatechange = complete;
xmlhttp.send(null);
}
注掉,先看看他能不能显示
function checkUsername(){
document.getElementById("userInfo").innerHTML="正在验证中………………";
alert("提示"); //只要加上这一句也可以看到效果;
var xmlhttp = createHttpObject();
if(xmlhttp){
var url = "/user.do?username=" + $("username").value;
xmlhttp.open("POST",url, false);
xmlhttp.onreadystatechange = complete;
xmlhttp.send(null);
}
}
标签换成<td><div id="userInfo"></div></td>试试
我换成JQuery的,可以了,再烦...难道又要去学JQuery包吗