我有这个模糊功能,如下所示,第一次不检查,第二次文字改变时,
<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 = "";
}
这是我的文本框:
<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>
我正在检查它:
<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
答案 0 :(得分:1)
包裹功能
$("#<%=txtUsername.ClientID %>").blur(function () { etc..
在
$(document).ready(function(){
//.. your code here
)};
可能是DOM没有加载,并且事件第一次没有绑定。 document.ready包装器将确保DOM已加载
答案 1 :(得分:0)
在keydown或keypress中编写此函数,然后keyup事件可能正在工作。