0

说我有以下功能:jQuery的 - 从函数返回一个值

function checkPanes() { 
    activePane = ''; 
    var panels = $("#slider .box .panel"); 

    panels.each(function() { 

    //find the one in visible state. 
    if ($(this).is(":visible")) { 
    activePane = $(this).index()+1; 
    console.log(activePane); 
    } 

    }); 
} //END checkPanes(); 

理想情况下,我想在这个函数调用别处(最有可能从另一个功能), 和检索价值我目前正在输出到控制台。

(例如..)

function exampleCase() { 
    checkPanes(); //evidently, does not return anything. 
    //Ideally, I want the numerical value, being output to console in above function. 
} 

提前感谢!所有的建议/意见非常感谢。
干杯

+0

你试过'返回activePane;'? – Sampson 2011-05-16 15:48:19

回答

2

忘记大家谁说return activePane因为他们没有看到它在一个jQuery each环。将无法工作。

我建议重构您的选择器。您应该使用的选择器是:$("#slider .box .panel:visible")。这将完全切断你的每个循环。举例来说,你可以重构代码如下:

function checkPanes() { 
    return $("#slider .box .panel:visible").index(); 
} 

function exampleCase() { 
    var visiblePane = checkPanes(); 

    // ... do something with the index 
} 

我建议只使用选择在线,而不是做一个新的功能,但是这是一个品味的问题,特别是如果你有选择相同在多个地方的事情。

+0

它可能处于循环中,但由于注释表明我们正在寻找处于可见状态的/ one/pane,所以一旦我们找到它,我们就可以退出循环。 – n00dle 2011-05-16 15:54:41

+0

相应修改。 – lsuarez 2011-05-16 15:57:35

+0

这是我试图完成的简化版本..谢谢! – 2011-05-16 16:01:56

4

刚注意到循环;看起来你可能想要返回的是所有活动面板的数组(因为理论上可能有多个面板)。

function checkPanes() { 
    activePanes = []; 
    var panels = $("#slider .box .panel"); 

    panels.each(function() { 

    //find the one in visible state. 
    if ($(this).is(":visible")) { 
    activePane.push($(this).index()+1); 
    console.log(activePane); 
    } 

    }); 
    return activePanes; 
} 

如果你知道有只将永远是一个活动,你可以回到你原来的做法,只是在console.log之后添加return activePane

+0

我认为是的..(我对这一切都很陌生) - 接下来的问题是,我将如何登录到控制台,该函数的“返回”值? 或在其他地方使用该返回值? (也许根据返回值创建一个新变量) - 干杯! – 2011-05-16 15:53:59

+0

如果在你的其他函数中做类似'var activePanes = checkPanes();'你将把返回值放到一个局部变量中,并且可以随意使用它(或者记录它)。 – 2011-05-16 15:57:31

0

控制台线只需切换到return语句:

function checkPanes() { 
    activePane = ''; 
    var panels = $("#slider .box .panel"); 

    panels.each(function() { 

    //find the one in visible state. 
    if ($(this).is(":visible")) { 
     activePane = $(this).index()+1; 
     return activePane; // Return the value and leave the function 
    } 

    }); 
} //END checkPanes(); 

要拨打:

function exampleCase() { 
    var thepane = checkPanes(); //evidently, does not return anything. 
    // ... 
} 
0

我认为这是那么容易,因为使用return activePane;

0

此:

function checkPanes() { 
    activePane = ''; 
    var panels = $("#slider .box .panel"); 

    panels.each(function() { 

    //find the one in visible state. 
    if ($(this).is(":visible")) { 
    activePane = $(this).index()+1; 
    } 

    }); 
    return activePane; 
} //END checkPanes(); 

这:

function exampleCase() { 
    var myval=checkPanes(); //evidently, does not return anything. 
    console.log(myval); 

}

+0

ahh ok,所以你必须将该函数传递给一个变量,然后检索它的值以便登录到控制台等。 谢谢! – 2011-05-16 16:00:20

+0

你并不总是需要。但是,大多数其他答案都会让您的原始函数返回时间较早,这可能相当糟糕(几乎肯定不是这种情况,但可能是现在或将来)。我所拥有的比其他大多数人更“安全”,这就是我使用这个变量的原因。 – ADW 2011-05-16 16:04:32

0

你必须返回的东西第一个函数内部操纵它的第二内侧:

function checkPanes() { 
    activePane = ''; 
    var panels = $("#slider .box .panel"); 
    //create a return array 
    visiblePanels = []; 
    panels.each(function() { 

    //find the one in visible state. 
    if ($(this).is(":visible")) { 
    activePane = $(this).index()+1; 
    //add the result to the returnb array 
    visiblePanels[] = activePane 
    } 
    }); 
    // return results 
    return visiblePanels; 
} 

function exampleCase() { 
    var thepane = checkPanes(); 
    //now it has all the visible panels that were founded in the other function 
    // you can access them with thepane[0] or iterate on them 

} 

我觉得这是你所需要的。

0

你可以把你的代码,只是如果你想使用的值别处

function checkPanes() { 
activePane = ''; 
var panels = $("#slider .box .panel"); 

    panels.each(function() { 

    //find the one in visible state. 
    if ($(this).is(":visible")) { 
    activePane = $(this).index()+1; 
    console.log(activePane); //Logs to console. 
    return activePane; //Returns value also. 
} 

}); 
} 

所以在这里你可以使用返回的值,或只是在用它登录到控制台添加回报。这就是我如何理解你的问题

function exampleCase() { 
    checkPanes(); //Now it will still write in console. but you dont need to use the return 

    alert(checkpanes()); //Would write it to console and to an alert! 
} 

但要确保你返回字符串 - 或者,如果你想不想找个disaply它作为文本转换为字符串。