ajax实现输入框提示信息

 

HtmlPage1.html

<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript">
        function loadXMLDoc(str)
        {
            var xmlhttp;
            if (str.length == 0)
            {
                document.getElementById("txtHint").innerHTML = "";
                return;
            }
            if (window.XMLHttpRequest)
            {
                xmlhttp = new XMLHttpRequest();
            }
            else
            {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.open("GET", "WebForm1.aspx?q="+str , true);
            xmlhttp.send();
            xmlhttp.onreadystatechange = callback;

            function callback()
            {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                {
                    document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                }
            }
        }
    </script>
</head>
<body>
    <form name="form1">
        <h3>请在输入框中输入字母(A-G):</h3>
        <input id="Text1" name="Text1" type="text" onkeyup="loadXMLDoc(this.value)" />
        <br />

        提示: <div id="txtHint"></div>
    </form>

</body>

WebForm1.aspx.cs 

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = -1;
            string[] a =
            {
                "Asdf","Awet","Bsdf","Bi","Csad","Coew",
                "Dasd","Dfg","Egfd","Efh","Fdsfh","Feth","Gsfgg","Grhf"
            };
            string q = Request.Params["q"].ToUpper();
            string hint = "";
            if (q.Length > 0)
            {
                int i;
                for (i = 0; i < 14; i++)
                {
                    string b = a[i].Substring(0, q.Length);
                    if (q == b)
                    {
                        if(hint == "")
                        {
                            hint = a[i];
                        }
                        else
                        {
                            hint = hint + "," + a[i];
                        }
                    }
                }
            }
            if (hint == "")
            {
                Response.Write("没有提示");
            }
            else
            {
                Response.Write(hint);
            }
            Response.End();
        }

 

猜你喜欢

转载自blog.csdn.net/qq_40323256/article/details/83992183