javascript jQuery $.each 变量作用域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10853547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:15:43  来源:igfitidea点击:

jQuery $.each variable Scope

javascriptjquery

提问by wesside

Having a scope issue problem with a $.eachloop in jQuery. How can I get a global variable in a function to set in a loop or at least pass something out of it?

jQuery 中$.each循环存在范围问题。如何在函数中获取全局变量以在循环中设置或至少从中传递一些内容?

var some_function = function() {

    // false by default
    var something = false;

    $.each(array, function(key, val) { 
       if (val == 'something')
       {
         // even if one item evaluates true I need to check outside of the loop
         something = true;
       }
    });

    if (something == true)
    {
       // do something else, but always false
    }
}

Since I'm needing to evaluate all items in the array, and if only one istrue, then do something additional, outside of the $.each.

由于我需要评估数组中的所有项目,如果只有一个是true,那么在$.each.

Update

更新

$(document).ready(function () {
    something(); 
    $(':radio').trigger('change');
)};

Ok, so this is the actual code. It's alerting 'false'at the bottom and then alerts 'hello'twice, as if it's going in reverse order.

好的,这是实际的代码。它在底部警告“假”,然后警告“你好”两次,就好像它以相反的顺序进行。

var something = function() {   

        var q_radios = {
            'radio1'        : 'radio1_selector', 
            'radio2'        : 'radio2_selector',  
        };
        var show_me = false;

        $.each(q_radios, function(name, q_selector) {  
            $('input:radio[name=' + q_selector + ']').change(function() {  
                    show_me = true; 
                    alert('hello'); 
            }); 
        });   

        if (show_me == true)
        {
            alert('yes');
        }
        else
        {
            alert('false');
        }

};

采纳答案by Quentin

More or less, exactly what you have now … you just need to pass the data into the function (by letting it accept an argument). See a live example.

或多或少,正是您现在拥有的……您只需要将数据传递给函数(通过让它接受一个参数)。查看一个活生生的例子

var some_function = function(array) { 

    // false by default
    var something = false;
    $.each(array, function(key, val) {
        if (val == 'something') {
            something = true;
        }
    });

    if (something == true) {
        alert("There was a something");
    } else {
        alert("There wasn't a something");
    }
};

some_function([1,2,3]);
some_function([1,"something",3]);
?

回答by ?ime Vidas

I recommend native Array iteration methods:

我推荐原生数组迭代方法:

var something = array.some(function ( val ) {
   return val === 'something';
});

Here, somethingwill be trueif at least one array element has the value 'something'.

这里,somethingtrue如果至少一个数组元素具有值'something'