将jQuery对象和WebMethod返回值传递给OnSuccess函数

时间:2020-11-30 18:59:06

I am calling a WebMethod from this code:

我从这段代码调用WebMethod:

if($(this).attr("checked")) {  
..  
MyWebMethod(variable1, variable2, onSuccessFunction);  
}

The MyWebMethod returns an integer, and I want to set $(this).attr("id") of the jQuery object above to the returned integer. Basically, I'm trying to do the equivalent of an MVC Ajax.ActionLink...AjaxOptions {UpdateTargetID =...} However, I can't figure out how to get both a reference to $(this) as well as the returned value. For example, if I do:

MyWebMethod返回一个整数,我想将上面jQuery对象的$(this).attr(“id”)设置为返回的整数。基本上,我正在尝试相当于MVC Ajax.ActionLink ... AjaxOptions {UpdateTargetID = ...}但是,我无法弄清楚如何获得对$(this)的引用以及返回值。例如,如果我这样做:

MyWebMethod(variable1, variable2, onSuccessFunction($(this)));

I can succesfully manipulate the jQuery object, but obviously it doesn't have the return value from the MyWebMethod. Alternatively, the first code block with a method signature of onSuccessFunction(returnValue) has the correct return value from MyWebMethod, but no concept of the jQuery object I'm looking for. Am I going about this all wrong?

我可以成功地操作jQuery对象,但显然它没有来自MyWebMethod的返回值。或者,具有onSuccessFunction(returnValue)方法签名的第一个代码块具有来自MyWebMethod的正确返回值,但是没有我正在寻找的jQuery对象的概念。我错了吗?

1 个解决方案

#1


0  

I don't know exactly what parameters the onSuccessFunction in your first example is expecting, but something like this will be what you're looking for.

我不确切知道你的第一个例子中onSuccessFunction的参数是什么,但是这样的东西将是你正在寻找的东西。

if($(this).attr("checked")) {   
  var el = $(this); 
  MyWebMethod(variable1, variable2, function(x, y z) { onSuccessFunction(x, y, z, el); });   
} 

** Update ** Fixed to avoid a "this" scoping issue.

**更新**已修复以避免出现“此”范围问题。

#1


0  

I don't know exactly what parameters the onSuccessFunction in your first example is expecting, but something like this will be what you're looking for.

我不确切知道你的第一个例子中onSuccessFunction的参数是什么,但是这样的东西将是你正在寻找的东西。

if($(this).attr("checked")) {   
  var el = $(this); 
  MyWebMethod(variable1, variable2, function(x, y z) { onSuccessFunction(x, y, z, el); });   
} 

** Update ** Fixed to avoid a "this" scoping issue.

**更新**已修复以避免出现“此”范围问题。