[英]How can i pass javascript addListener function value to change function inside
I have addListener
function and change
function is there. 我有
addListener
函数和change
函数在那里。 How can I pass addListener
result to change
function inside using javascript 如何使用JavaScript通过
addListener
结果change
函数
$(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. 这里有
fun1
和fun2
两个功能。 How can i pass fun1
pincode
value to fun2
inside? 如何传递
fun1
pincode
值fun2
里面?
You already have defined pincode in global scope. 您已经在全局范围内定义了密码。 Just use it.
只需使用它。 Here
function fun2(val)
val is Event
object passed to event callback. 这里的
function fun2(val)
val function fun2(val)
是传递给事件回调的Event
对象。 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);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.