<title>ajax用户验证</title>
<head>
<script language="javascript">function check()
{

if(document.getElementById("name").value!="")
{
var xmlHttp;
function S_xmlhttprequest(){
               xmlHttp=null;
               if (window.XMLHttpRequest){// code for IE7, Firefox, Opera, etc.
                 xmlHttp=new XMLHttpRequest();
                 }
                else if (window.ActiveXObject){// code for IE6, IE5
                 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                 }
}
//xmlHttp.open("GET","date1.php?name="+document.getElenentById("name").value,true);
xmlHttp.onreadystatechange = function();
{
/*if(xmlHttp.readyState ==1)
{
              document.getElementById("name1").innerHTML = "检测中......";
}
*/
if(xmlHttp.status == 200 && xmlHttp.readyState ==4)
{

    var name = xmlHttp.responseText;
document.getElementById("name1").innerHTML =name;

}
}
xmlHttp.open("GET","date1.php?name="+document.getElenentById("name").value,true);
//xmlHttp.onreadystatechange = byname;
   xmlHttp.send(null);
    //xmlHttp.send(null);
}
}
else
{
document.getElementById("name1").innerHTML="用户名不能为空";
//exit;
}
}function pwd()
{
if(document.getElementById("pwd2").value=="")
{
document.getElementById("pwd1").innerHTML="密码不能为空";
//exit;
}
else
{
document.getElementById("pwd1").innerHTML="该密码强度.....";
}
}
</script>
</head>
<body>
<table align="center">
<tr><td>用户名:</td><td><input type="text" name="name" id="name" value="" onblur="check()"/>
</td><td><div id="name1"><font color="#CC66CC">*用户名必填*</font></div></td></tr>
<tr><td>密码:</td><td><input type="text" name="pwd2" id="pwd2" value="" onblur="pwd()"/>
</td><td><div id="pwd1"><font color="#CC66CC">*密码必填*</font></div></td></tr>
</table>
</body>为什么老是提示在21行有个错误就是 “缺少 {”?
希望帮忙给看看

解决方案 »

  1.   

    这里:var xmlHttp;
            function S_xmlhttprequest(){
                xmlHttp=null;
                if (window.XMLHttpRequest){// code for IE7, Firefox, Opera, etc.
                    xmlHttp=new XMLHttpRequest();
                }
                else if (window.ActiveXObject){// code for IE6, IE5
                    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
            }你定义了创建xmlHttpRequest对象的函数,但该函数没有被调用吧?也就是xmlHttp没有创建……
      

  2.   

    xmlHttp.onreadystatechange = function();
    分号加错地方了吧
      

  3.   


    <title>ajax用户验证</title>
    <head>
    <script language="javascript">
    function $(id){return doxument.getElementById(id)}
    function check(){
    $("name1").innerHTML = "检测中......";}
      if($("name").value==""){$("name1").innerHTML="用户名不能为空"; return;}
      var xmlHttp=new XMLHttpRequest()||new ActiveXObject("Microsoft.XMLHTTP");
      if(!xmlHttp){
       alert('创建AJAX对象失败!');  return;
      }
      
      xmlHttp.onreadystatechange = function(){
        if(xmlHttp.readyState==4{
          if(xmlhttp.status==200||xmlhttp.status==0){
            var ret = xmlHttp.responseText;
            $("name1").innerHTML =ret;    
          }
        }
        $("name1").innerHTML = "检测失败!";}
      }
      xmlHttp.open("GET","date1.php?name="+$("name").value+"&"+new Date().getTime(),true);
      xmlHttp.send(null);
    }function pwd(){
    var msg=$("pwd2").value=="密码不能为空"?"该密码强度.....";
      $("pwd1").innerHTML=msg;
    }
    </script>
    </head>
    <body>
    <table align="center">
    <tr><td>用户名:</td><td><input type="text" name="name" id="name" value="" onblur="check()"/>
    </td><td><div id="name1"><font color="#CC66CC">*用户名必填*</font></div></td></tr>
    <tr><td>密码:</td><td><input type="text" name="pwd2" id="pwd2" value="" onblur="pwd()"/>
    </td><td><div id="pwd1"><font color="#CC66CC">*密码必填*</font></div></td></tr>
    </table>
    </body>
      

  4.   

    3楼有手误,看这个。
    <title>ajax用户验证</title>
    <head>
    <script language="javascript">
    function $(id){return document.getElementById(id)}
    function check(){
      $("name1").innerHTML = "检测中......";}
      if($("name").value==""){$("name1").innerHTML="用户名不能为空"; return;}
      var xmlHttp=new XMLHttpRequest()||new ActiveXObject("Microsoft.XMLHTTP");
      if(!xmlHttp){
        alert('创建AJAX对象失败!');  return;
      }
      
      xmlHttp.onreadystatechange = function(){
        if(xmlHttp.readyState==4{
          if(xmlhttp.status==200||xmlhttp.status==0){
            var ret = xmlHttp.responseText;
            $("name1").innerHTML =ret;    
          }
        }
        $("name1").innerHTML = "检测失败!";}
      }
      xmlHttp.open("GET","date1.php?name="+$("name").value+"&"+new Date().getTime(),true);
      xmlHttp.send(null);
    }function pwd(){
      var msg=$("pwd2").value=="密码不能为空"?"该密码强度.....";
      $("pwd1").innerHTML=msg;
    }
    </script>
    </head>
    <body>
    <table align="center">
    <tr><td>用户名:</td><td><input type="text" name="name" id="name" value="" onblur="check()"/>
    </td><td><div id="name1"><font color="#CC66CC">*用户名必填*</font></div></td></tr>
    <tr><td>密码:</td><td><input type="text" name="pwd2" id="pwd2" value="" onblur="pwd()"/>
    </td><td><div id="pwd1"><font color="#CC66CC">*密码必填*</font></div></td></tr>
    </table>
    </body>