[英]how to pass multiple arguments to onSuccess function in Prototype?
我是使用Prototype库的初学者。 我想知道如何将多个参数传递给Prototype中的onSuccess / onFailure函数? 例如:-
new Ajax.Request('testurl',{
method: 'post',
parameters: {param1:"A", param2:"B", param3:"C"},
onSuccess: fnSccs,
onFailure: fnFail
})
在我的成功函数fnSccs: -
function fnSccs(response)
{
alert(response.responseText);
}
我想将一个新参数传递给fnSccs函数。 怎么可能。 谢谢你的帮助。
您可以将成功函数包装到另一个接收所需参数的函数中,并返回旧函数:
new Ajax.Request('testurl',{
method: 'post',
parameters: {param1:"A", param2:"B", param3:"C"},
onSuccess: mySuccess('myValue1', 'myValue2'),
onFailure: fnFail
})
function mySuccess(param1, param2){
return function(response){ // Your old success function
alert(param1); // The parameter still accessible here
alert(param2);
alert(response);
}
}
会发生什么事情,当你调用mySuccess(...)
会返回旧函数,但是你仍然可以访问参数,因为变量仍然在外部闭包上分配。
您可以在此处查看正在运行的代码段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.