开发者

Simple jQuery .change question

开发者 https://www.devze.com 2023-02-04 09:35 出处:网络
I am new to jQuery and am having difficulty getting a .change event to call a named func开发者_运维技巧tion.When I use an anonymous function, it works fine.

I am new to jQuery and am having difficulty getting a .change event to call a named func开发者_运维技巧tion. When I use an anonymous function, it works fine.

This works fine:

$(function() {

    $("select").change(function() {
        alert("hello");
    }); 
});

This does not work (i.e. has not effect):

$(function() {
    $("select").change(processSelection());
    function processSelection() {alert('Hello!');}; 
});

Any assistance much appreciated!


You are passing the function as an argument, not calling it, so you don't need the () after processSelection. So, you should do

$("select").change(processSelection);

instead of

$("select").change(processSelection());
0

精彩评论

暂无评论...
验证码 换一张
取 消