Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript访问循环外的变量_Javascript_Loops_Closures_Nested Loops - Fatal编程技术网

使用Javascript访问循环外的变量

使用Javascript访问循环外的变量,javascript,loops,closures,nested-loops,Javascript,Loops,Closures,Nested Loops,我需要使用Javascript(而不是jQuery或其他js框架)在另一个函数中访问循环中的变量 这是我的js: for (x in drinks) { for (y = 0; y < drinks[x].length; y++) { // testVarGlobal = drinks[x][y].name; doesn't work if (drinks[x][y].type == 'type') { txt['type'] =

我需要使用Javascript(而不是jQuery或其他js框架)在另一个函数中访问循环中的变量

这是我的js:

for (x in drinks) {
    for (y = 0; y < drinks[x].length; y++) {
    // testVarGlobal = drinks[x][y].name; doesn't work
        if (drinks[x][y].type == 'type') {

            txt['type'] = '<span>' + drinks[x][y].name + ' - ' + drinks[x][y].price +'</span>';
            document.getElementById("type").innerHTML += txt['type'];

        } // endif
    } // end for
} // end for in

function foo() {
    // on the next line I need the value from the loop - drinks[x][y].name
    var something = document.getElementById(testVarGlobal);
    return something.value;
}
用于(饮料中的x){
对于(y=0;y<饮料[x]。长度;y++){
//testVarGlobal=drinks[x][y].name;不起作用
if(饮料[x][y]。类型=='type'){
txt['type']=''+饮料[x][y]。名称+'-'+饮料[x][y]。价格+'';
document.getElementById(“type”).innerHTML+=txt['type'];
}//endif
}//结束
}//结束于
函数foo(){
//在下一行中,我需要循环中的值-drinks[x][y].name
var something=document.getElementById(testVarGlobal);
回报某物。价值;
}

当前代码片段很难分辨。也许你是说

for (x in drinks) {
    for (y = 0; y < drinks[x].length; y++) {
        if (drinks[x][y].type == 'type') {
            txt['type'] = '<span>' + foo(drinks[x][y].name) + ' - ' + drinks[x][y].price +'</span>';
            document.getElementById("type").innerHTML += txt['type'];
        } // endif
    } // end for
} // end for in

function foo(drinkName) {
    var something = document.getElementById(drinkName);
    return something.value;
}
所以你需要

function getUpdatedQty(id) { 
  var prodQty = document.getElementById(id); 
  return prodQty?prodQty.value:0; // handle an ID that does not exist on page
}

调试使用

function getUpdatedQty(id) { 
  var prodQty = document.getElementById(id); 
  var val = prodQty?prodQty.value:0;
  window.console && console.log(id,prodQty,val);
  return val;
}
点击F12查看控制台

如果您有id:4并且需要ID004,您可以使用

function pad(num) {
  return "ID"+("000"+num).slice(-3)
}

var testVarGlobal='';
用于(饮料中的x){
对于(y=0;y<饮料[x]。长度;y++){
testVarGlobal=饮料[x][y]。名称;
if(饮料[x][y]。类型=='type'){
txt['type']=''+饮料[x][y]。名称+'-'+饮料[x][y]。价格+'';
document.getElementById(“type”).innerHTML+=txt['type'];
}//endif
}//结束
}//结束于
var somethingValue=foo();
函数foo(){
//在下一行中,我需要循环中的值-drinks[x][y].name
var something=document.getElementById(testVarGlobal);
回报某物。价值;
}

foo(某物)在哪里?就在for loopWhere的外部?它不在代码段中调用,我将其作为另一个函数的参数调用,如果
var something=document.getElementById('someidthatexists')工作正常,但这还不够,因为我必须传递[x][y].name或not,这取决于他调用foo的位置和testVarGlobal的来源@Chris-发布更多代码并停止发明FunctionNames我将修改我的代码,看看这种方法是否有效;如果我给你发个链接会有帮助吗?这是一个简单/小的js文件-GetUpdatedQuantity是一个必须返回所提到的值的函数,我已经如图所示对其进行了修改,但由于某种原因,数量没有传递;我还在玩它-我得到了一个7,null,0-7是ID,0应该是数量-jvfmaa/index.htmlet我们得到了一个
引用错误:testVarGlobal没有定义
@ChrisDemetriad,发布更多的代码,这样我们可以提供更多帮助。您是如何定义testVarGlobal的?@ChrisDemetriad,根据您的逻辑,您可能需要将testVarGlobal初始化为某个值,或者在使用它之前在foo()中进行检查以确保它具有值。根据需要全局变量的位置,可能不需要全局变量。把全局范围弄得乱七八糟被认为是不好的做法——特别是如果你只需要通过它的话。@mplungjan,我同意,但正如你所说,这要视情况而定。而且,由于他似乎不想分享任何进一步的代码,这仍然是一种选择
function getUpdatedQty(id) { 
  var prodQty = document.getElementById(id); 
  var val = prodQty?prodQty.value:0;
  window.console && console.log(id,prodQty,val);
  return val;
}
function pad(num) {
  return "ID"+("000"+num).slice(-3)
}
  var prodQty = document.getElementById(pad(id)); 
var testVarGlobal = '';

for (x in drinks) {
    for (y = 0; y < drinks[x].length; y++) {
        testVarGlobal = drinks[x][y].name;
        if (drinks[x][y].type == 'type') {

            txt['type'] = '<span>' + drinks[x][y].name + ' - ' + drinks[x][y].price +'</span>';
            document.getElementById("type").innerHTML += txt['type'];

        } // endif
    } // end for
} // end for in

var somethingValue = foo();

function foo() {
    // on the next line I need the value from the loop - drinks[x][y].name
    var something = document.getElementById(testVarGlobal);
    return something.value;
}