什么是错这个模糊的功能? [英] Whats wrong with this blur function?
本文介绍了什么是错这个模糊的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这种模糊功能如下图所示,在第一次这么想的检查,当文本改变了它的工作原理,第二次
<脚本类型=文/ JavaScript的>
功能ShowAvailability(){
变种的userName = $(#&下;%= txtUsername.ClientID%gt;中);
$(#<%= txtUsername.ClientID%GT;)。模糊(函数(){
USERNAME = $(本).VAL(); 如果(userName.length小于5){
$(#MESG)[0] .innerHTML =;
}
其他{
$阿贾克斯({
键入:POST,
网址:Default.aspx的/ CheckUserName
数据:{用户名:'+ $(#<%= txtUserName.ClientID%GT;)[0] .value的+'}
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON
成功:的onSuccess,
故障:功能(响应){
警报(响应);
}
});
}
});
}
功能的onSuccess(响应){
变种MESG = $(#MESG)[0]; 开关(response.d){
案真:
mesg.style.color =绿色;
mesg.innerHTML =可用;
打破;
情况下假:
mesg.style.color =红;
mesg.innerHTML =不可用;
打破;
案错误:
mesg.style.color =红;
mesg.innerHTML =发生错误;
打破;
}
}
功能的OnChange(TXT){
$(#MESG)[0] .innerHTML =;
}
这是我的文本框:
< ASP:文本框ID =txtUsername=服务器边框=实边框宽度=1px的BORDERCOLOR =#0099CC背景色=#FAFFBDAutoCompleteType =已禁用的onkeyup =的OnChange(本)的onblur =ShowAvailability()>< / ASP:文本框>
< ASP:的RequiredFieldValidator ID =UserNameRequired=服务器
的ControlToValidate =txtUsername的ErrorMessage =用户名是必需的。
工具提示=用户名是必需的。
的CssClass =signupvalidators前景色=红> * LT; / ASP:&的RequiredFieldValidator GT;
&所述;跨度的id =MESG>&下; /跨度>
在这里,我检查它:
< System.Web.Services.WebMethod()> _
公共共享功能CheckUserName(BYVAL用户名作为字符串)作为字符串
昏暗的returnValue作为字符串=的String.Empty
尝试
昏暗consString作为字符串= _ ConfigurationManager中
.ConnectionStrings(conString)。ConnectionString的
昏暗康恩作为新的SqlConnection(consString)
昏暗CMD作为新的SqlCommand(spx_CheckUserAvailability,康涅狄格州)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue(@用户名,userName.Trim())
conn.Open()
的returnValue = cmd.ExecuteScalar()的ToString()
conn.Close()
抓住
的returnValue =错误
结束Try
返回的returnValue
结束功能
解决方案
裹功能 P>
$(#<%= txtUsername.ClientID%GT;)。模糊(函数(){等。
在
$(文件)。就绪(函数(){ // ..您code在这里)};
这可能是因为在DOM没有加载,而该事件不通过约束的第一次。该包装的document.ready将确保DOM加载
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
解决方案
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
这篇关于什么是错这个模糊的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文