如何在PageMethods回调中将上下文对象作为数组进行访问

时间:2021-09-25 19:57:47

I cannot seem to access the context object using a loop context is set: var context = [id1, id2, id3];

我似乎无法使用循环上下文来访问上下文对象:var context = [id1,id2,id3];

This callback function works:

此回调函数有效:

function OnChangeSucceeded(result, context, methodName) {
    document.getElementById(context[0]).disabled = result;
    document.getElementById(context[1]).disabled = result;
    document.getElementById(context[2]).disabled = result;
}

This callback function fails:

此回调函数失败:

function OnChangeSucceeded(result, context, methodName) {
        for(var indx = 0; indx < context.length; indx++) {
           document.getElementById(context[indx]).disabled = result;
        }

    }

3 个解决方案

#1


It would be handy to see the calling code so that we could see how your context is established. I'm going to guess that you've set it up as an association and not an array so that when you go to use it in the callback, there is no length property (or it's 0).

查看调用代码以便我们可以看到您的上下文是如何建立的,这将非常方便。我猜你已经将它设置为关联而不是数组,这样当你在回调中使用它时,就没有长度属性(或者它是0)。

When you set it up it should look like:

设置时,它应该如下所示:

var context = new Array();
context[0] = 'elem0';
context[1] = 'elem1';
context[2] = 'elem2';

not

var context = { 0: 'elem0', 1: 'elem1', 2: 'elem2' };

var context = {0:'elem0',1:'elem1',2:'elem2'};

If that isn't the problem, then try checking it out in FireFox/FireBug by setting a breakpoint in the onChangeSucceeded function and examining the actual context object to see what properties it has.

如果这不是问题,那么尝试通过在onChangeSucceeded函数中设置断点并检查实际的上下文对象以查看它具有的属性,在FireFox / FireBug中检出它。

#2


Is it because of your typo?

是因为你的错字吗?

for(var index = 0; indx < context.length; indx++) {

should be

for(var indx = 0; indx < context.length; indx++) {

#3


Thats for the pointer to firebug tvanfosson.

这是指向firebug tvanfosson的指针。

I have redone the function and it now works as:

我重做了这个功能,现在它的工作原理如下:

function OnChangeSucceeded(result, context, methodName) {
    for (controlId in context) {
        document.getElementById(context[controlId]).disabled = result;
    }
}

I am not sure if it was because the context was original created as:

我不确定是否因为上下文原始创建为:

context = [id1, id2, id3];

which I have now replaced with:

我现在替换为:

context = new Array(id1, id2, id3);

#1


It would be handy to see the calling code so that we could see how your context is established. I'm going to guess that you've set it up as an association and not an array so that when you go to use it in the callback, there is no length property (or it's 0).

查看调用代码以便我们可以看到您的上下文是如何建立的,这将非常方便。我猜你已经将它设置为关联而不是数组,这样当你在回调中使用它时,就没有长度属性(或者它是0)。

When you set it up it should look like:

设置时,它应该如下所示:

var context = new Array();
context[0] = 'elem0';
context[1] = 'elem1';
context[2] = 'elem2';

not

var context = { 0: 'elem0', 1: 'elem1', 2: 'elem2' };

var context = {0:'elem0',1:'elem1',2:'elem2'};

If that isn't the problem, then try checking it out in FireFox/FireBug by setting a breakpoint in the onChangeSucceeded function and examining the actual context object to see what properties it has.

如果这不是问题,那么尝试通过在onChangeSucceeded函数中设置断点并检查实际的上下文对象以查看它具有的属性,在FireFox / FireBug中检出它。

#2


Is it because of your typo?

是因为你的错字吗?

for(var index = 0; indx < context.length; indx++) {

should be

for(var indx = 0; indx < context.length; indx++) {

#3


Thats for the pointer to firebug tvanfosson.

这是指向firebug tvanfosson的指针。

I have redone the function and it now works as:

我重做了这个功能,现在它的工作原理如下:

function OnChangeSucceeded(result, context, methodName) {
    for (controlId in context) {
        document.getElementById(context[controlId]).disabled = result;
    }
}

I am not sure if it was because the context was original created as:

我不确定是否因为上下文原始创建为:

context = [id1, id2, id3];

which I have now replaced with:

我现在替换为:

context = new Array(id1, id2, id3);