javascript 在函数外传递变量。查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8725371/
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
Pass variable outside of function. JQuery
提问by Cecil Theodore
I am trying to pass the variable
checkBoxClasses
outside of the current function.
我试图传递variable
checkBoxClasses
当前函数的外部。
How can I do this?
我怎样才能做到这一点?
$(currentMap).find(':checkbox').filter(':checked').each(function () {
var b = $(this).attr('class').split(' ');
var checkBoxClasses = b[0];
});
alert(checkBoxClasses);
回答by Darin Dimitrov
You could use an array to which append the results:
您可以使用将结果附加到的数组:
var classes = new Array();
$(currentMap).find(':checkbox').filter(':checked').each(function () {
var b = $(this).attr('class').split(' ');
classes.push(b[0]);
});
alert(classes);
Another possibility is to use the .map
function which seems more adapted to your requirements:
另一种可能性是使用.map
似乎更适合您的要求的功能:
var classes = $(currentMap).find(':checkbox:checked').map(function () {
return $(this).attr('class').split(' ')[0];
}).toArray();
alert(classes);
回答by Marcus
This happens because of JS variable scope. More info for ex. What is the scope of variables in JavaScript?
这是因为 JS 变量作用域。前的更多信息。JavaScript 中变量的作用域是什么?
In general a variable cannot be used outside the scope in which it's defined.
通常,变量不能在定义它的范围之外使用。
That's why this should work.
这就是为什么这应该起作用。
var checkBoxClasses = [];
$(currentMap).find(':checkbox').filter(':checked').each(function () {
var b = $(this).attr('class').split(' ');
checkBoxClasses.push(b[0]);
});
alert(checkBoxClasses);
In your version the anonymous function inside each-loop means variables defined inside that function are not visible outside that function.
在您的版本中,每个循环内的匿名函数意味着在该函数内定义的变量在该函数外不可见。
回答by Christofer Eliasson
You would have to declare the variable outside the function scope, otherwise the variable will be private to the anonymous function.
您必须在函数作用域之外声明该变量,否则该变量将是匿名函数的私有变量。
var checkBoxClasses;
$(currentMap).find(':checkbox').filter(':checked').each(function () {
var b = $(this).attr('class').split(' ');
checkBoxClasses = b[0];
});
alert(checkBoxClasses);
However, as you are looping over the checkboxes, only the last checkbox will be alert, if you do it this way. If you like to do an alert for every checkbox, you would have to move the alert into the loop as well. Or, if you would like to alert all the checkboxes in a single alert, you could use either an array, or extend the string with each new checkbox, instead of replacing it entirely.
但是,当您遍历复选框时,如果您这样做,则只有最后一个复选框会发出警报。如果你想为每个复选框做一个警报,你也必须将警报移动到循环中。或者,如果您想在单个警报中提醒所有复选框,您可以使用数组,或使用每个新复选框扩展字符串,而不是完全替换它。
回答by Snake Eyes
Why not :
为什么不 :
var checkBoxClasses; // "global" variable, outside function
$(currentMap).find(':checkbox').filter(':checked').each(function () {
var b = $(this).attr('class').split(' ');
checkBoxClasses = b[0];
});
alert(checkBoxClasses);
回答by kaz
For declaring global variables from any scope you should use window.variable syntax:
要从任何范围声明全局变量,您应该使用 window.variable 语法:
$(currentMap).find(':checkbox').filter(':checked').each(function () {
var b = $(this).attr('class').split(' ');
window.checkBoxClasses = b[0];
});