[英]jQuery .click( foo() ) unexpected behavior
Today I came across a bug in my web app regarding form search submit function (in js) and a button click action in jQuery.今天我在我的 web 应用程序中遇到了一个关于表单搜索提交功能(在 js 中)和 jQuery 中的按钮单击操作的错误。
I have a form that has the action submitEmpSearch()
attached to it.我有一个表单,其中附加了submitEmpSearch()
操作。 This is embedded in the html of the form as it's onSubmit
action.这嵌入在表单的 html 中,因为它是onSubmit
操作。 There is also a search button associated with the form with an onclick
event submitEmpSearch()
.还有一个与带有onclick
事件submitEmpSearch()
的表单相关联的搜索按钮。 I was making an attempt to clear out embedded javascript from the templates in favor of jQuery calls.我试图从模板中清除嵌入的 javascript 以支持 jQuery 调用。 In an included jQuery call I had the call $('#search-button').click( submitEmpSearch() )
inside my $(document).ready
group.在包含的 jQuery 调用中,我在$(document).ready
组中调用了$('#search-button').click( submitEmpSearch() )
。
From my previous experience with the .click()
function, I expected this to have the same functionality as the embedded onclick
attribute provided.根据我之前使用.click()
函数的经验,我希望它具有与提供的嵌入式onclick
属性相同的功能。 However, instead, it gave the page an infinite loop of (apparently) clicking the button as soon as it loaded!然而,相反,它给页面一个无限循环(显然)一加载就点击按钮!
Now, previously, I always passed a function() {}
pattern.现在,以前,我总是传递一个function() {}
模式。 I had never passed in a predefined function before.我以前从未传入过预定义的函数。 Is this some kind of gotcha in the jQuery这是 jQuery 中的某种陷阱吗language语framework?框架? Or is there something else I'm doing wrong?还是我做错了什么?
jQuery is not a language. jQuery 不是一种语言。 JavaScript is the language. JavaScript是一种语言。 You're passing the result of the function, when you want to pass the function itself:当您想传递函数本身时,您正在传递函数的结果:
It should be:它应该是:
$('#search-button').click(submitEmpSearch)
In JavaScript, functions can be passed as values.在 JavaScript 中,函数可以作为值传递。 However, submitEmpSearch()
is the return value of a call to the function, as one would expect from other languages.然而, submitEmpSearch()
是函数调用的返回值,正如人们对其他语言所期望的那样。
Instead of代替
$('search-button').click( submitEmpSearch() )
use just只用
$('#search-button').click( submitEmpSearch )
or或者
$('#search-button').click( function(){submitEmpSearch();} )
Also if the 'search-button' is a class you have to add a "."此外,如果“搜索按钮”是一个类,您必须添加一个“。” (dot) before it. (点)在它之前。 If it is an id you have to add "#".如果是 id,则必须添加“#”。 See my examples.看我的例子。
Aha!啊哈!
You should pass the function by reference, not call the function and pass its result:您应该通过引用传递函数,而不是调用函数并传递其结果:
function handler(){
// ...
}
jQuery('.something').click(handler()); // <- wrong
jQuery('.something').click( handler ); // <- right
Change from:更改自:
$('search-button').click(submitEmpSearch())
to:到:
$('search-button').click(submitEmpSearch)
This way instead of executing submitEmpSearch() and passing its return value to the click(), you're passing the delegate to the click().这种方式不是执行 submitEmpSearch() 并将其返回值传递给 click(),而是将委托传递给 click()。
Try this.尝试这个。
$('#search-button').click(function(){
submitEmpSearch();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.