简体   繁体   English

如何从javascript / jQuery中调用另一个函数的函数获取返回值?

[英]How can I get the return value from a function calling another function in javascript/jQuery?

So you can assign a value to var doub as below: 因此,您可以为var doub分配一个值,如下所示:

function getDouble(number) {
    var doubl = number + number;
    return doubl;
}

var num = 4;
var doub = getDouble(num);

I want to know if I can get the return type by calling a second function as below. 我想知道是否可以通过调用第二个函数来获取返回类型,如下所示。 So can the return value of the second method be assigned to doub ? 那么可以将第二种方法的返回值赋给doub吗?

function getDouble(number) {
    var doubl = number + number;
    return quadruple(doubl);
}

function quadruple(doubl) {
    quad = doubl + doubl
    return quad;
}

var num = 4;
var doub = getDouble(num);

For some reason it doesn't work for me. 由于某种原因,它对我不起作用。

This is my code with the problem..I am doing something similar to the above, but not working: 这是我的问题代码。我正在做与上面类似的操作,但是不起作用:

In my doc ready, I have this code..I want the result to be stored in newjsn 在我准备好的文档中,我有以下代码..我希望将结果存储在newjsn中

    $(document).ready(function() 
    {
     var newjsn= DatabaseCategoriesToSelectList(num, 200, 100);

My DatabaseCategoriesToSelectList function is : 我的DatabaseCategoriesToSelectList函数是:

function DatabaseCategoriesToSelectList(jsn, max, min){

    var ddl="<option selected='selected' value='"+min+"'>Select...</option>";

    //load categories from database, and put them into the select drop down.
    $.getJSON(GLOBALURL+'content/plugins/subcategories/getSubcategories.php',             
        function(data, ddl) {

            var datas=  jQuery.grep(data, function(element, index){
                return element.category_id < max && element.category_id>min; // retain appropriate elements
            });
            for (x=0; x<datas.length; x++){
                ddl= ddl+"<option value='"+datas[x].category_id+"'data-subcategory='"+datas[x].category_subselect+"'>"+datas[x].category_safe_name+"</option>";
            }   
            alert (ddl); return runafter(ddl, jsn); 
        });

}

function runafter(ddl, jsn){
    jsn=ddl;
    alert(jsn);

    return 500;

}

For some reason, the alerts work, but I can't get it to return me the string. 出于某种原因,警报可以正常工作,但是我无法通过它返回字符串。 I even tried to return a random value of 500, and it doesnt.. 我什至尝试返回500的随机值,但没有。

The problem is that you're never returning out of your DatabaseCategoriesToSelectList function. 问题在于您永远不会退出DatabaseCategoriesToSelectList函数。 Your function looks like this: 您的函数如下所示:

function DatabaseCategoriesToSelectList(jsn, max, min) {
    ...
    $.getJSON(GLOBALURL+'content/plugins/subcategories/getSubcategories.php',             
        function(data, ddl) {
             ...
             return runafter(ddl, jsn); 
        }
    );
}

The inner return is not returning out of DatabaseCategoriesToSelectList , it is returning out of the inner function. 内部return不是从DatabaseCategoriesToSelectList中返回,而是从内部函数中返回。 However, there is no way to make this return the values that you want no matter what you do, because $.getJSON is an asynchronous call. 但是,无论您做什么,都无法使其返回所需的值,因为$.getJSON是异步调用。 You have to put something like 你必须放一些东西

function(data, dll) {
    ...
    var newjsn = runafter(ddl, jsn);
    // put the rest of your stuff that uses newjsn here
}

in your code, and then put whatever came after that also into that function also. 在您的代码中,然后将之后的所有内容也放入该函数中。

Fiddle ( output is : 16 ) 小提琴输出为:16

What you are doing is just fine as it should 您正在做的就应该很好

 function getDouble (number){
    var doubl = number+number;
    return quadruple(doubl);
    }

function quadruple(doubl){
var quad=doubl+doubl
return quad;
}

var num = 4;
var doub = getDouble(num);

You aren't going to have access to the return from a getJson handler. 您将无法访问getJson处理程序的返回值。 The framework calls that handler. 框架调用该处理程序。 You are giving it a function, and .getJson calls that function if/when the response is received. 您正在给它一个函数,如果/当收到响应时,.getJson会调用该函数。 If you need access to the return of runAfter, you need to do whatever with it you plan to. 如果需要访问runAfter的返回值,则需要执行计划执行的任何操作。

Instead of: 代替:

$.getJSON(GLOBALURL+'content/plugins/subcategories/getSubcategories.php',
  function(data, ddl) {

          ...
           return runafter(ddl, jsn); 
  });
}

You need to decide what you want to do with that value, for example I stuff it into the value of some hidden field maybe: 您需要决定要使用该值做什么,例如,我可能将其填充到某个隐藏字段的值中:

$.getJSON(GLOBALURL+'content/plugins/subcategories/getSubcategories.php',
  function(data, ddl) {

          ...
          $('#someField').val( runafter(ddl, jsn) ); 
  });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何调用javascript函数并从javascript函数获取返回值 - How I can call javascript function and get the return value from javascript function 如果参数是数字,我怎样才能让雪花中的 javascript 函数返回一个值,如果参数不是数字,我如何返回另一个值? - How can I get a javascript function in snowflake to return one value if the argument is numeric or another value if the argument is not numeric? 从另一个JavaScript函数调用jQuery函数 - calling a jQuery function from another JavaScript function 如何获取jquery post函数的返回值? - How can I get the return value of jquery post function? 如何获取Android调用的JavaScript函数的返回值? - How can I get the return value of a JavaScript function called by Android? 如何在我的 html 文件中调用异步 javascript function 在另一个文件中获取返回值? - How to get return value in my html file calling async javascript function in another file? 如何从 JavaScript 中的 Handler 函数获取返回值? - How do I get a return value from a Handler function in JavaScript? 如何从javascript函数中的.then内部返回值? - How can I return a value from within a .then inside a javascript function? 如何从javascript中的这个子函数返回值 - How can I return value from this child function in javascript 如何从包含jQuery.get()的JavaScript函数中获取返回值? - How can I get returned value from JavaScript function that contain jQuery.get()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM