Whats wrong with this blur function?

帅比萌擦擦* 提交于 2019-12-13 06:53:47

问题


I have this blur function as shown below and on first time it dosen't check and when text changed for the second time it works

<script type = "text/javascript">
    function ShowAvailability() {
    var userName = $("#<%=txtUsername.ClientID %>");
    $("#<%=txtUsername.ClientID %>").blur(function () {
        userName = $(this).val();

        if (userName.length < 5) {
            $("#mesg")[0].innerHTML = "";
        }
        else {
            $.ajax({
                type: "POST",
                url: "Default.aspx/CheckUserName",
                data: '{userName: "' + $("#<%=txtUserName.ClientID%>")[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 = "";
    }

This is my textbox:

 <asp:TextBox ID="txtUsername" runat="server" BorderStyle="Solid" BorderWidth="1px" BorderColor="#0099CC" BackColor="#FAFFBD" AutoCompleteType="Disabled" onkeyup = "OnChange(this)" Onblur="ShowAvailability()"></asp:TextBox>
             <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
             ControlToValidate="txtUsername" ErrorMessage="User Name is required." 
             ToolTip="User Name is required." 
             CssClass="signupvalidators" ForeColor="Red">*</asp:RequiredFieldValidator>
             <span id = "mesg"></span>

Here I'm checking it:

 <System.Web.Services.WebMethod()> _
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

回答1:


Wrap the function

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

In

$(document).ready(function(){

  //.. your code here

)};

It could be that the DOM hasn't loaded and the event isn't binding the first time through. The document.ready wrapper will ensure the DOM has loaded




回答2:


write this function in keydown or keypress ratherthen keyup event may be it's working.



来源:https://stackoverflow.com/questions/8939911/whats-wrong-with-this-blur-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!