Javascript jQuery .blur() 事件未触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13667864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery .blur() event not firing
提问by Hyman
I have a simple login form like below, with an attached jQuery script. I'm trying to make some simple client-side form validation using jQuery. I can't seem to get the blur event to fire, however. I can't think of what I'm doing wrong, I've tried using '.on('blur', fn(){})' but that doesn't work either. Please help!
我有一个简单的登录表单,如下所示,附带一个 jQuery 脚本。我正在尝试使用 jQuery 进行一些简单的客户端表单验证。但是,我似乎无法触发模糊事件。我想不出我做错了什么,我试过使用 '.on('blur', fn(){})' 但这也不起作用。请帮忙!
Here's my HTML file:
这是我的 HTML 文件:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>DMIT 222 - Catalogue Project: Admin Login</title>
<link rel="stylesheet" type="text/css" href="../styles/divs.css" />
<link rel="stylesheet" type="text/css" href="../styles/fonts.css" />
<link rel="stylesheet" type="text/css" href="../styles/tags.css" />
<link rel="stylesheet" type="text/css" href="../styles/tables.css" />
<link rel="stylesheet" type="text/css" href="styles/forms.css" />
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="../scripts/loginFormValidation.js"></script>
</head>
<body>
<div id="wrapper">
<header>
<h1>Ship Catalogue</h1>
</header>
<div id="mainContent">
<form id="LoginForm" name="LoginForm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="FormRow"><label for="Username">Username:</label><input type="text" name="Username" id="Username" /><p id="userErrors"><?php echo $userErrors; ?></p></div>
<div class="FormRow"><label for="Password">Password:</label><input type="password" name="Password" id="Password" /><p id="passwordErrors"><?php echo $passwordErrors; ?></p></div>
<div class="FormRow"><input type="submit" name="Submit" value="Login!" /></div>
</form>
</div>
<footer>
<a href="http://www.logicanddesign.ca/">Site Developed by Logic & Design</a>
</footer>
</div>
</body>
</html>
And here's the loginFormValidation.js script:
这是 loginFormValidation.js 脚本:
$("#Username").blur(function(e){
alert();
if ($("#Username").val() == "") {
$("#userErrors").text("Username must be entered!");
}
});
$("#Password").blur(function(e){
if ($("#Password").val() == "") {
$("#passwordErrors").text("Password must be entered!");
}
});
采纳答案by Sudhir Bastakoti
Either add your jquery blur code inside
要么在里面添加你的 jquery 模糊代码
$(document).ready(function() { ...// your blur code here }
OR at bottom of your page above closing bodytag like:
或在您的页面底部结束正文标签上方,例如:
<script type="text/javascript">
//your blur code
</script>
</body>
回答by Christopher.Cubells
You need to wrap your events inside jQuery's document.ready function:
您需要将事件包装在 jQuery 的 document.ready 函数中:
$(document).ready(function() {
$("#Username").blur(function(e){
alert();
if ($("#Username").val() == "") {
$("#userErrors").text("Username must be entered!");
}
});
$("#Password").blur(function(e){
if ($("#Password").val() == "") {
$("#passwordErrors").text("Password must be entered!");
}
});
});
回答by Kevin Bowersox
It works if you add some content to alert. The JS engine threw an error because the alert function was being called with no parameters. I would also recommend binding the event with document.ready()
如果您向警报添加一些内容,它会起作用。JS 引擎抛出一个错误,因为调用了没有参数的警报函数。我还建议将事件与document.ready()
$(document).ready(function(){
$("#Username").blur(function(e){
alert("something");
if ($("#Username").val() == "") {
$("#userErrors").text("Username must be entered!");
}
});
});
Example: http://jsfiddle.net/PfnGv/

