零基础学习AJAX之制作自动校验的表单(2)


<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<%@ Import Namespace="System.Data" %>
<%
    Response.CacheControl = "no-cache";
    Response.AddHeader("Pragma","no-cache");
   
    if(Request["user"]=="isaac")
        Response.Write("Sorry, " + Request["user"] + " already exists.");
    else
        Response.Write(Request["user"]+" is ok.");
%>

4.显示异步查询的结果

在用户输入表单其它项目时,异步返回结果已经在后台悄悄完成。

复制代码 代码如下:


function showResult(sText) {
                var oSpan = document.getElementById("UserResult");
                oSpan.innerHTML = sText;
                if (sText.indexOf("already exists") >= 0)
                //如果用户名已被占用
                    oSpan.style.color = "red";
                else
                    oSpan.style.color = "black";
            }

以上代码是对服务器返回结果的显示。

该案例的完整代码

复制代码 代码如下:


<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <script type="text/javascript">
            var xmlHttp;
            function createXMLHttpRequest() {
                if (window.ActiveXObject)
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                else if (window.XMLHttpRequest)
                    xmlHttp = new XMLHttpRequest();
            }
            function showResult(sText) {
                var oSpan = document.getElementById("UserResult");
                oSpan.innerHTML = sText;
                if (sText.indexOf("already exists") >= 0)
                //如果用户名已被占用
                    oSpan.style.color = "red";
                else
                    oSpan.style.color = "black";
            }
            function startCheck(oInput) {
                //首先判断是否有输入,没有输入直接返回,并提示
                if (!oInput.value) {
                    oInput.focus(); //聚焦到用户名的输入框
                    document.getElementById("UserResult").innerHTML = "用户名不能为空";
                    return;
                }
                //创建异步请求
                createXMLHttpRequest();
                var sUrl = "1-9.aspx?user=" + oInput.value.toLowerCase() + "&timestamp=" + new Date().getTime();
                xmlHttp.open("GET", sUrl, true);
                xmlHttp.onreadystatechange = function() {
                    if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
                        showResult(xmlHttp.responseText); //显示服务器结果
                }
                xmlHttp.send(null);
            }
        </script>
        <form>
            <p>
                <label for="User">输用户名
                    <input type="text" onblur="startCheck(this)">
                </label><span></span>
            </p>
            <p>
                <label for="passwd1">输入密码
                    <input type="password">
                </label>
            </p>
            <p>
                <label for="passwd2">重复输入
                    <input type="password">
                </label>
            </p>
            <p>
                <input type="submit" value="注册">
            </p>
            <p>
                <input type="reset" value="重置">
            </p>
        </form>
    </body>
</html>

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wfypdp.html