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

javascript - Pass a function as parameter in jQuery? - Stack Overflow

programmeradmin2浏览0评论

I would like to pass to a jQuery function a regular function, instead of the usual anonymous function, but I'm not sure how such a thing could be done.

Instead of this:

function setVersion(feature) {
      $.post("some.php", { abc:"abc" },
      function(data){
         // do something here
      }, "json");
}

I would like to do this:

function foo(data){
   // do something here
}

function setVersion(feature) {
      $.post("some.php", { abc:"abc" }, foo, "json");
}

Thank you.

I would like to pass to a jQuery function a regular function, instead of the usual anonymous function, but I'm not sure how such a thing could be done.

Instead of this:

function setVersion(feature) {
      $.post("some.php", { abc:"abc" },
      function(data){
         // do something here
      }, "json");
}

I would like to do this:

function foo(data){
   // do something here
}

function setVersion(feature) {
      $.post("some.php", { abc:"abc" }, foo, "json");
}

Thank you.

Share Improve this question asked Apr 17, 2010 at 20:52 thedpthedp 8,50816 gold badges57 silver badges100 bronze badges 3
  • 9 Ummm... run that. – harpo Commented Apr 17, 2010 at 20:54
  • 2 Yep. That should work exactly as you want. Give it a try ;) – Edurne Pascual Commented Apr 17, 2010 at 20:57
  • LOL, my bad, I tried to run "foo", didn't bother to run it as just foo... And then I forgot the "" in the question :) I need to get some sleep!!! – thedp Commented Apr 17, 2010 at 20:59
Add a comment  | 

3 Answers 3

Reset to default 12

Yeah, already works. But you want it probably look like this:

function setVersion(feature, myFunction) {
      $.post("some.php", { abc:"abc" }, myFunction, "json");
}
setVersion(blah, foo);

Should run just fine.

I believe jQuery is actually meant to use the regular function, called by name. Using the anonymous function is simply a replacement for a named function that would otherwise be passed.

Yes, that is exactly how you do it.

发布评论

评论列表(0)

  1. 暂无评论