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.

Share Improve this question edited Jun 8, 2012 at 7:50 scessor 16.1k4 gold badges44 silver badges55 bronze badges asked Jun 8, 2012 at 7:43 MuntingInsektoMuntingInsekto 1,5934 gold badges19 silver badges37 bronze badges 4
  • 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 the checked property to get their state. – Guffa Commented Jun 8, 2012 at 7:55
Add a ment  | 

3 Answers 3

Reset to default 3

Define "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