这个模糊function有什么问题?

我有这个模糊function,如下所示,并在第一次它不检查和文本改变第二次它工作

 function ShowAvailability() { var userName = $("#"); $("#").blur(function () { userName = $(this).val(); if (userName.length < 5) { $("#mesg")[0].innerHTML = ""; } else { $.ajax({ type: "POST", url: "Default.aspx/CheckUserName", data: '{userName: "' + $("#")[0].value + '" }', contentType: "application/json; charset=utf-8", dataType: "json", success: OnSuccess, failure: function (response) { alert(response); } }); } }); } function OnSuccess(response) { var mesg = $("#mesg")[0]; switch (response.d) { case "true": mesg.style.color = "green"; mesg.innerHTML = "Available"; break; case "false": mesg.style.color = "red"; mesg.innerHTML = "Not Available"; break; case "error": mesg.style.color = "red"; mesg.innerHTML = "Error occured"; break; } } function OnChange(txt) { $("#mesg")[0].innerHTML = ""; } 

这是我的文本框:

   *  

我正在检查它:

   _ Public Shared Function CheckUserName(ByVal userName As String) As String Dim returnValue As String = String.Empty Try Dim consString As String = ConfigurationManager _ .ConnectionStrings("conString").ConnectionString Dim conn As New SqlConnection(consString) Dim cmd As New SqlCommand("spx_CheckUserAvailability", conn) cmd.CommandType = CommandType.StoredProcedure cmd.Parameters.AddWithValue("@UserName", userName.Trim()) conn.Open() returnValue = cmd.ExecuteScalar().ToString() conn.Close() Catch returnValue = "error" End Try Return returnValue End Function 

包裹function

  $("#<%=txtUsername.ClientID %>").blur(function () { etc.. 

 $(document).ready(function(){ //.. your code here )}; 

可能是DOM没有加载,并且事件第一次没有绑定。 document.ready包装器将确保DOM已加载

在keydown或keypress中写这个函数,然后keyup事件可能正在工作。