我是jQuery的新手,并且很难获得.change事件来调用命名函数.当我使用匿名函数时,它工作正常.
这很好用:
$(function() {
$("select").change(function() {
alert("hello");
});
});
Run Code Online (Sandbox Code Playgroud)
这不起作用(即没有效果):
$(function() {
$("select").change(processSelection());
function processSelection() {alert('Hello!');};
});
Run Code Online (Sandbox Code Playgroud)
任何帮助非常感谢!
您将函数作为参数传递,而不是调用它,因此您不需要()after processSelection.所以,你应该这样做
$("select").change(processSelection);
Run Code Online (Sandbox Code Playgroud)
代替
$("select").change(processSelection());
Run Code Online (Sandbox Code Playgroud)