简体   繁体   中英

How can i pass javascript addListener function value to change function inside

I have addListener function and change function is there. How can I pass addListener result to change function inside using javascript

$(document).ready(function() {
  // find pincode
  var input = document.getElementById('location_input');
  var options = {
    types: ['address'],
    componentRestrictions: {
      country: 'in'
    }
  };
  autocomplete = new google.maps.places.Autocomplete(input, options);
  var pincode;

  google.maps.event.addListener(autocomplete, 'place_changed', function fun1() {
    var place = autocomplete.getPlace();
    for (var i = 0; i < place.address_components.length; i++) {
      for (var j = 0; j < place.address_components[i].types.length; j++) {
        if (place.address_components[i].types[j] === "postal_code") {
          pincode = place.address_components[i].long_name;
          fun2(pincode);
          //alert(pincode);
        } //return pincode;
      }
    }
  });

  $('input[type="checkbox"],#location_input').change(function fun2(val) {
    var ids = ['filter_AFFILIATION_1', 'filter_AFFILIATION_2', 'filter_AFFILIATION_3', 'filter_AFFILIATION_4'];
    var data = {};
    for (var i = 0; i < ids.length; i++) {
      if (document.getElementById(ids[i]).checked === true) {
        data['request' + i] = $('#' + ids[i]).val();
      }
    }
    var pincode = val;
    alert(pincode);
    console.log(pincode);
  });
});

Here fun1 and fun2 two functions. How can i pass fun1 pincode value to fun2 inside?

You already have defined pincode in global scope. Just use it. Here function fun2(val) val is Event object passed to event callback. And you dont have to use named functions, anon funcs will work as event callbacks.

$(document).ready(function () {
    // find pincode
    var input = document.getElementById('location_input');
    var options = {
        types: ['address'],
        componentRestrictions: {
            country: 'in'
        }
    };
    autocomplete = new google.maps.places.Autocomplete(input, options);
    var pincode;
    google.maps.event.addListener(autocomplete, 'place_changed', function () {
        var place = autocomplete.getPlace();
        for (var i = 0; i < place.address_components.length; i++) {
            for (var j = 0; j < place.address_components[i].types.length; j++) {
                if (place.address_components[i].types[j] === "postal_code") {
                    pincode = place.address_components[i].long_name;
                } //return pincode;
            }
        }
    });
    $('input[type="checkbox"]').change(function (event) {
        var ids = ['filter_AFFILIATION_1', 'filter_AFFILIATION_2', 'filter_AFFILIATION_3', 'filter_AFFILIATION_4'];
        var data = {};
        for (var i = 0; i < ids.length; i++) {
            if (document.getElementById(ids[i]).checked === true) {
                data['request' + i] = $('#' + ids[i]).val();
            }
        }
        alert(pincode);
        console.log(pincode);
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM