Ajax实现异步刷新验证用户名是否已存在的具体方

都是简单的实例,所以直接发代码

静态页面Ajax.html

复制代码 代码如下:


<html>
    <head>
        <title>Ajax</title>
        <script type="text/javascript">
            function loadXMLDoc() {
                if (document.getElementById("account").value == "") {
                    document.getElementById("accDiv").innerHTML = "用户名不能为空";
                    return;
                }
                var xmlHttp;
                if(window.XMLHttpRequest) { // code for IE7+
                    xmlHttp = new XMLHttpRequest();
                }
                else { // code for IE5/IE6
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }

xmlHttp.onreadystatechange = function () {
                    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                        //document.getElementById("myDiv").innerHTML=xmlHttp.responseText;
                        if (xmlHttp.responseText == "true") {
                            document.getElementById("accDiv").innerHTML = "用户名不可用";
                        }
                        else {
                            document.getElementById("accDiv").innerHTML = "用户名可用";
                        }
                    }
                }
                var a = document.getElementById("account").value;
                // get
                xmlHttp.open("GET", "validate.aspx?account=" + a + "&random=" + Math.random, true);
                xmlHttp.send();
            }
            function delData() {
                document.getElementById("account").value = "";
                document.getElementById("accDiv").innerHTML = "";
            }
        </script>
    </head>
    <body>
        <h3>ajax</h3>
        <table>
            <tr>
                <td>账号:</td><td><input type="text" onblur="loadXMLDoc();" onfocus="delData();"/></td><td><div></div></td>
            </tr>
            <tr>
                <td>密码:</td><td><input type="password" /></td>
            </tr>
            <tr>
                <td>确认密码:</td><td><input type="password" /></td>
            </tr>
            <tr>
                <td>姓名:</td><td><input type="text" /></td>
            </tr>
        </table>

    </body>
</html>


在账号输入框失去焦点时调用函数

访问服务器使用的是Get方法,所以在参数处使用了附加随机码来避免缓存。

验证页面validate.aspx后台代码:

复制代码 代码如下:

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

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