简体   繁体   English

jQuery .blur()不起作用

[英]JQuery .blur() Not Working

Basically, my Blur() function in Jquery is not being activated. 基本上,我的Jquery中的Blur()函数没有被激活。 My code is below and I'm using a special .notify jquery addition. 我的代码在下面,并且我正在使用特殊的.notify jQuery添加。 I don't think there is anything wrong the actual body of the function but its more to do with the .blur() function itself. 我认为函数的实际主体没有什么错,但更多与.blur()函数本身有关。

 $(document).ready(function(e) { $("#verifypassword").blur(function(e) { var password1=document.getElementById("password").value; var password2=document.getElementById("verifypassword").value; if(password1!=password2){ $("#verifypassword").notify("Passwords do not match."); } else{ $("#verifypassword").notify("Passwords match!"); } }); }); 
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>The KGV Connection</title> <link rel="stylesheet" type="text/css" href="stylesheet.css" /> <script src="jquery-2.1.1.min.js"></script> <script src="animation.js"></script> <script src="notify.js"></script> </head> <body> <script type="text/javascript"> function makeitpassword(){ var passform= document.getElementById("passcontainer"); passform.innerHTML="<input type=\\"password\\" class=\\"signuser\\"id=\\"password\\" value=\\"\\"/>"; document.getElementById("password").focus(); } function makeitpassword2(){ var passform= document.getElementById("passcontainer2"); passform.innerHTML="<input type=\\"password\\" class=\\"signuser\\" id=\\"verifypassword\\" value=\\"\\" onfocus=\\"makeitpassword2()\\"/>"; document.getElementById("verifypassword").focus(); } </script> <div class="signup"> <h1 class="signheader">Sign Up</h1> <br /> <form id="signup" action="signup.php" method="post"> <input type="text" class="signuser" name="username" value="Username"/> <div id="passcontainer"> <input type="text" class="signuser" value="Password" id="password" onfocus="makeitpassword()" name="Password"/> </div> <div id="passcontainer2"> <input type="text" class="signuser" id="verifypassword" onfocus="makeitpassword2()" value="Repeat Password" name="VerifyPassword"/> </div> <input type="email" class="signuser" value="Email" name="Email"/> <br /> <br /> <input type="submit" value="Sign Up" class="logsubmit"/> </form> </div> </body> 

Your onblur isn't working because it isn't set to the password element. 您的onblur无法正常运行,因为未将其设置为password元素。 When a user clicks on your password inputs they get replaced by a new one with your makeitpassword#() functions. 当用户单击您的密码输入时,他们将被具有makeitpassword#()函数的新密码替换。 This causes the original element to be removed and the new element doesn't has a blur attached to it. 这将导致原始元素被删除,而新元素没有附加模糊效果。 Lose the onfocus events and you'll be fine! 失去onfocus事件,您会没事的!

If you want to display a message in the input use placeholder : 如果要在输入中使用placeholder显示消息:

 <input type="password" placeholder="password here"> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM