开发者

Pass a function as parameter in jQuery?

开发者 https://www.devze.com 2022-12-27 11:44 出处:网络
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.

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.


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

精彩评论

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