为什么这个javascript函数在不被调用的情况下运行? [英] Why is this javascript function running without being called?
本文介绍了为什么这个javascript函数在不被调用的情况下运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$(document).ready(SetupButtonClicks());
function SetupButtonClicks() {
$('#btnJavaPHP').click(DoPHPStuff());
}
function DoPHPStuff() {
//stuff
}
我的javascript文件中包含此代码,当我对其进行调试时,我看到它像应该的那样调用SetupButtonClicks()
,但是完成后它将调用DoPHPStuff()
.仅当单击btnJavaPHP
时,才应调用DoPHPStuff()
.我在做什么错了?
I have this code in my javascript file, when I debug it I see it call SetupButtonClicks()
like it should but after that is done it calls DoPHPStuff()
. DoPHPStuff()
should only be called when btnJavaPHP
is clicked. What am I doing wrong?
推荐答案
更改SetupButtonClicks函数:
Change your SetupButtonClicks function:
$('#btnJavaPHP').click(DoHPStuff);
您编写代码的方式是,告诉Javascript 调用该函数,而不是使用作为"click"处理程序.括号是运算符,它会导致函数被调用.
The way you've got it coded, you're telling Javascript to call the function, not to use it as the "click" handler. The parentheses are an operator that causes a function to be called.
这篇关于为什么这个javascript函数在不被调用的情况下运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文