最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - call multiple functions from single event with unobtrusive javascript - Stack Overflow

programmeradmin1浏览0评论

I have an input element

<select id="test"></select>

on change I want to call multiple functions

$('#test').change(function1, function2);

functions for now are just alerts for now

var function1 = function(){alert('a');};
var function2 = function(){alert('b');};

Only the second function is being called. I know this because of alerts and brake points. I know one way to correct this would be to call function1, and function2 from another function, but I would like to avoid that.

I have an input element

<select id="test"></select>

on change I want to call multiple functions

$('#test').change(function1, function2);

functions for now are just alerts for now

var function1 = function(){alert('a');};
var function2 = function(){alert('b');};

Only the second function is being called. I know this because of alerts and brake points. I know one way to correct this would be to call function1, and function2 from another function, but I would like to avoid that.

Share Improve this question asked May 7, 2013 at 21:07 dan_vitchdan_vitch 4,55912 gold badges48 silver badges69 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 6

I prefer not to use anonymous functions so I would create a new function and place all work inside it.

$('#test').change(onSelectChange);

var onSelectChange = function() {
    function1();
    function2();
}

You can always chain them:

$('#test').change(function1).change(function2);

You can do this -

$('#test').change(function1);
$('#test').change(function2);

You can do it like this:

$('#test').change(function(){function1(); function2();});

Why not try something like:

$('#test').change(function(){
   function1();
   function2();
};
发布评论

评论列表(0)

  1. 暂无评论