【问题标题】:jquery - returning a value from functionjquery - 从函数返回一个值
【发布时间】:2011-08-26 13:34:49
【问题描述】:

假设我有以下功能:

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.
}  

提前致谢!非常感谢所有建议/cmets。
干杯

【问题讨论】:

  • 你试过return activePane;吗?

标签: javascript jquery function return-value javascript-framework


【解决方案1】:

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

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。

【讨论】:

  • 我认为是的..(我对这一切都很陌生)-我的下一个问题是,我将如何登录到控制台,该函数的“返回”值?或者在其他地方使用该返回值? (也许根据返回值创建一个新变量) - 干杯!
  • 如果在您的其他函数中执行var activePanes=checkPanes(); 之类的操作,您会将返回值放入一个局部变量中,并且可以随意使用它(或记录它)。
【解决方案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
}

我建议只使用内联选择器而不是创建一个新函数,但这只是个人喜好问题,尤其是当您必须在多个地方选择相同的东西时。

【讨论】:

  • 它可能在一个循环中,但由于评论表明我们正在寻找处于可见状态的 /one/ 窗格,一旦我们找到它,我们就可以退出循环。
  • 这是我想要完成的简化版本。谢谢!
  • 是的!呃!如此简单...我无法在其他任何地方得到这个简单的答案... 简短答案:如果您将函数构造为像 var data = get_data() 这样的 var 而不是 get_data() 那么您可以使用数据你得到了同样的功能。
【解决方案3】:

只需将控制台行切换到返回语句即可:

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. 
    // ...
}  

【讨论】:

    【解决方案4】:

    我觉得就像使用return activePane;一样简单

    【讨论】:

      【解决方案5】:

      这个:

      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);
      

      }

      【讨论】:

      • 啊,好吧,所以你必须将函数传递给一个变量,然后再检索它的值以记录到控制台等。谢谢!
      • 您并不总是需要这样做。但是,大多数其他答案都会让您的原始函数提前返回,这可能非常糟糕(在这种情况下几乎可以肯定不是,但可能是现在或将来)。我所拥有的比大多数其他人“更安全”,这就是我使用该变量的原因。
      【解决方案6】:

      如果您想在其他地方使用这些值,您可以保留您的代码并添加一个返回

      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!
      } 
      

      但请确保您返回字符串 - 如果您想将其作为文本显示在某处,请确保将其转换为字符串。

      【讨论】:

        【解决方案7】:

        你必须在第一个函数中返回一些东西才能在第二个函数中操作它:

        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
        
        }  
        

        我想这就是你需要的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-19
          • 1970-01-01
          • 2015-05-25
          • 2011-02-06
          • 2020-03-19
          • 1970-01-01
          相关资源
          最近更新 更多