点击函数在页面加载时触发?
试图弄清楚这怎么可能
$(function() {
$('#saveBtn').click(save());
});
function save(){
alert('uh');
}
......
<form id="video-details" method="post" action="">
<input id="saveBtn" class="submit" type="submit" name="submit" value="Submit Video" disabled/>
</form>
我在 save()
函数上设置了一个断点,并且它由列出了单击事件的行上的匿名函数调用。然而,这是直接在加载时发生的,更不用说输入开始时是不可见和禁用的,因此无法单击它。
我将点击功能更改为存在和不存在的不同元素,无论我做什么,该功能仍然会在 pageload
上触发。
不知道还能在哪里调查造成这种情况的原因,但我假设这不是正常行为
Trying to figure out how this is possible...
$(function() {
$('#saveBtn').click(save());
});
function save(){
alert('uh');
}
.
<form id="video-details" method="post" action="">
<input id="saveBtn" class="submit" type="submit" name="submit" value="Submit Video" disabled/>
</form>
I set a breakpoint on the save()
function and it is being called by an anonymous function on the line with the click event listed. However this is happening directly on load, not to mention that the input starts off invisible and disabled so there is no way to click it.
I changed the click function to different elements that both exist and don't exist and whatever I make it the function still fires on pageload
.
Not sure where else to investigate the cause of this, but I'm assuming it's not normal behaviour
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将脚本更改为:
通过在
click
声明中使用方括号来调用该函数。如果您只使用函数名称,那么您将提供对该函数的引用而不是调用。使用变量调用
如果您使用变量调用函数,则需要使用闭包(假设您在声明事件时有权访问该变量)
有关闭包的更多信息,请查看 JavaScript 闭包是如何工作的?。
Try changing your script to:
By having brackets in the
click
declaration you are calling the function. If you just use the function name then you are providing a reference to the function instead of a call.Calling with variable
If you are calling the function with a variable you would need to make use of a closure (assuming that you have access to the variable when declaring the event
For more information on closures check out How do JavaScript closures work?.
尝试更改为这个。您意外调用了 click
try changing to this. You are invoking click accidentally