admin管理员组文章数量:1426058
$("#div").click(function(){
$('#myselect').change(function() {
var names = [];
$('.checkbox input').each(function() {
if(this.value != ''){
names.push(this.value);
}
});
console.log(names);
});
});
I need to use the names
outside that function to use it on another function. Can anyone tell me how to do it?
$("#dropdown").change(function(){
console.log(names);
});
names
is an array.
$("#div").click(function(){
$('#myselect').change(function() {
var names = [];
$('.checkbox input').each(function() {
if(this.value != ''){
names.push(this.value);
}
});
console.log(names);
});
});
I need to use the names
outside that function to use it on another function. Can anyone tell me how to do it?
$("#dropdown").change(function(){
console.log(names);
});
names
is an array.
- What's your other function? What's the purpose you're trying to achieve? – Florian Margaine Commented Jun 8, 2012 at 7:46
- And the reason you can't move the array to an outer scope is...? make them share the same scope by putting them inside the same ready handler and the array should be declared at top of the ready handler. – gdoron Commented Jun 8, 2012 at 7:48
- You are attaching the change event inside the click event, which means that you first have to click the #div element, then change the value in the #myselect control for the array to be filled. Is that really what you want, as what you do inside the change event doesn't depend on either the #div element or the #myselect control? – Guffa Commented Jun 8, 2012 at 7:50
-
Also, are the elements that you target with
.checkbox input
checkboxes? In that case their value will never change due to user input. You would need to use thechecked
property to get their state. – Guffa Commented Jun 8, 2012 at 7:55
3 Answers
Reset to default 3Define "names" outside the function...
var names = [];
$('#myselect').change(function() {
$('.checkbox input').each(function() {
if(this.value != ''){
names.push(this.value);
}
});
console.log(names);
});
You should call the other function from that change-event handler. Defining the array outside of it would make no sense, it is bound to that event.
function gotNewNames(names) {
// do something every time the value changes
}
$("#div").click(function(){
$('#myselect').change(function() {
var names = [];
$('.checkbox input').each(function() {
if(this.value != ''){
names.push(this.value);
}
});
gotNewNames(names);
});
});
If you just need to be able to get the current values somewhere else, you should declare the variable in an higher scope, e.g. in the click handler:
$("#div").click(function(){
var names;
$('#myselect').change(function() {
names = []; // don't forget to reset before filling
$('.checkbox input').each(function() {
if(this.value != ''){
names.push(this.value);
}
});
});
$("#dropdown").change(function(){
console.log(names);
});
});
You mean like this:
Please let me know if I missed anything.
Otherwise hope this helps,
Code
var names = []; // Move the name array to outer scope
$("#div").click(function(){
$('#myselect').change(function() {
$('.checkbox input').each(function() {
if(this.value != ''){
names.push(this.value);
}
});
console.log(names);
});
alert(names);
});
本文标签: javascriptHow can I get the array outside the functionStack Overflow
版权声明:本文标题:javascript - How can I get the array outside the function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745461571a2659346.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论