[英]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. 在您的代码中,然后将之后的所有内容也放入该函数中。
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.