[英]JQuery not executing defined function in .blur()
I'm very new to JQuery and Javascript in general, and I'm working on a project for school. 我对JQuery和Javascript一般都很陌生,我正在为学校做一个项目。 While I can get everything to work without defining any functions, I want to reduce redundant code. 虽然我可以在不定义任何功能的情况下完成所有工作,但我希望减少冗余代码。
Here's a bit of what I'm trying to do: 这是我正在尝试做的一些事情:
function firstNameChanged() {
var x = document.getElementById("firstName").value;
if (x == null || x == "") {
$("#firstNameError").html(" First name can't be blank!");
}
else {
$("#firstNameError").html("");
}
}
$(document).ready(function(){
$("#firstName").blur(firstNameChange());
}
Yes I'm positive all IDs exist, because the code works when I pass it as: 是的,我肯定所有的ID都存在,因为代码在我传递给它时起作用:
$("#firstName").blur(function(){
var x = document.getElementById("firstName").value;
if (x == null || x == "") {
$("#firstNameError").html(" First name can't be blank!");
}
else {
$("#firstNameError").html("");
}
})
I've looked up plenty of documentation on Javascript and JQuery syntax, debugged in Chrome, I can't figure it out. 我查了大量关于Javascript和JQuery语法的文档,在Chrome中调试过,我无法弄明白。
blur
expects a function reference as a parameter. blur
期望函数引用作为参数。 firstNameChange()
is calling the function immediately. firstNameChange()
立即调用该函数。 Remove the ()
删除()
$("#firstName").blur(firstNameChange);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.