jquery 向回调函数传参数
来源:互联网 发布:depthmap软件下载 编辑:程序博客网 时间:2024/04/20 03:11
The solution is the binding of variables through closure.
I haven't used the .post function in jQuery, but a quick scan of the document suggests the call back should be a function pointer accepting the following:
function callBack(data, textStatus) {};
Therefore I think the solution is as follows:
var doSomething = function(extraStuff) { return function(data, textStatus) { // do something with extraStuff };};var clicked = function() { var extraStuff = { myParam1: 'foo', myParam2: 'bar' }; // an object / whatever extra params you wish to pass. $.post("someurl.php", someData, doSomething(extraStuff), "json");};
What is happening?
In the last line, doSomething(extraStuff)
is invoked and the result of that invocation is a function pointer.
Because extraStuff
is passed as an argument to doSomething
it is within scope of thedoSomething
function.
When extraStuff
is referenced in the returned anonymous inner function of doSomething
it is bound by closure to the outer function's extraStuff
argument. This is true even after doSomething
has returned.
I haven't tested the above, but I've written very similar code in the last 24 hours and it works as I've described.
You can of course pass multiple variables instead of a single 'extraStuff' object depending on your personal preference/coding standards.
- jquery 向回调函数传参数
- jQuery中on方法向回调函数传递参数
- wxpython, 向BIND的回调函数传参数
- jquery ajax回调函数带入参数
- jquery回调函数
- 回调函数 参数传递
- 浅析jQuery---回调函数
- JQuery ajax回调函数
- jquery中的回调函数
- 83 jquery 回调函数
- jquery Ajax回调函数
- Jquery 中的回调函数
- jquery 中的回调函数
- jquery 中回调函数
- as3中如何向Alert.show的回调函数传递参数
- jquery多个参数以及参数个数不一样的回调函数(使用hash对象)
- 向dwr回调函数传多个参数
- 如何向回调函数中传参数
- 线段树题型汇总
- 【数据结构与算法】排序算法——快速排序
- Matlab符号运算
- 转载:TPC-H 使用
- MongoDB基本操作
- jquery 向回调函数传参数
- 【转载】偶是程序员
- 2013.9.7
- 04-windows的搜索功能的应用
- UVa 11396 - Claw Decomposition(二分图判定,染色法,dfs)
- UVA10817-----Headmaster's Headache-----状态压缩的背包(记忆化搜索实现)
- 进程控制
- JSP拔萃
- 博客系统不好用!