Ajax練習:使用jQuery驗證用戶名是否存在

1.頁面部分javascript


用戶名:<asp:TextBox ID="txtId" runat="server"></asp:TextBox>html

<asp:Label ID="lblShow" runat="server" ForeColor="Red"></asp:Label>

java

2.編寫javascript腳本部分jquery

<script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>

<script type="text/javascript">
$(function () {
$("#txtId").blur(function () {
var a = $("#txtId").val();
$.ajax({
type: "post",
url: "Handler1.ashx",
data: { m: a },
success: function (result) {
var res = result.toString();
$("#lblShow").html(res);
}
});
});
});
</script>

3.ashx中代碼ajax

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string Name = context.Request.Params["m"].ToString();

            if (AdminManager.nameIsExist(Name) == ture)
            {
                context.Response.Write("此用戶名已被註冊!");
            }
            else
            {
                context.Response.Write("此用戶名能夠使用!");
            }
        }
相關文章
相關標籤/搜索