Javascript 如何检查字段是否已动态填充?
我用谷歌地图做了一个计算器,显示出发地和目的地之间的固定价格 但价格只有在离开输入并单击地图或div后才会显示Javascript 如何检查字段是否已动态填充?,javascript,jquery,google-maps,Javascript,Jquery,Google Maps,我用谷歌地图做了一个计算器,显示出发地和目的地之间的固定价格 但价格只有在离开输入并单击地图或div后才会显示 $(document).ready(function(){ function initMap() { var latlng = new google.maps.LatLng(52.379189, 4.899431); var mapOptions = { center: latlng, zoom: 11, mapType
$(document).ready(function(){
function initMap() {
var latlng = new google.maps.LatLng(52.379189, 4.899431);
var mapOptions = {
center: latlng,
zoom: 11,
mapTypeControl: false,
streetViewControl: false,
styles: [{"featureType":"water","stylers":[{"saturation":43},{"lightness":-11},{"hue":"#0088ff"}]},{"featureType":"road","elementType":"geometry.fill","stylers":[{"hue":"#ff0000"},{"saturation":-100},{"lightness":99}]},{"featureType":"road","elementType":"geometry.stroke","stylers":[{"color":"#808080"},{"lightness":54}]},{"featureType":"landscape.man_made","elementType":"geometry.fill","stylers":[{"color":"#ece2d9"}]},{"featureType":"poi.park","elementType":"geometry.fill","stylers":[{"color":"#ccdca1"}]},{"featureType":"road","elementType":"labels.text.fill","stylers":[{"color":"#767676"}]},{"featureType":"road","elementType":"labels.text.stroke","stylers":[{"color":"#ffffff"}]},{"featureType":"poi","stylers":[{"visibility":"off"}]},{"featureType":"landscape.natural","elementType":"geometry.fill","stylers":[{"visibility":"on"},{"color":"#b8cb93"}]},{"featureType":"poi.park","stylers":[{"visibility":"on"}]},{"featureType":"poi.sports_complex","stylers":[{"visibility":"on"}]},{"featureType":"poi.medical","stylers":[{"visibility":"on"}]},{"featureType":"poi.business","stylers":[{"visibility":"simplified"}]}]
};
map = new google.maps.Map(document.getElementById('map'), mapOptions);
}
function drivingRoute(from, to) {
var request = {
origin: from,
destination: to,
travelMode: google.maps.DirectionsTravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC
};
if(typeof(drivingLine) !== 'undefined') drivingLine.setMap(null);
directionsService.route(request, function(response, status){
if(status == google.maps.DirectionsStatus.OK){
var totalKM = Math.round(response.routes[0].legs[0].distance.value / 1000);
var distanceText = totalKM+' km';
$('#controls p').text(distanceText);
var stadsdeelTest = document.getElementById('stadsdeel').value;
if (stadsdeelTest=='Centrum') {
var charges= '€ '+ 32;
}
else if (stadsdeelTest=='Amsterdam-Oost') {
var charges= '€ '+ 35;
}
else if (stadsdeelTest=='Oud-Zuid') {
var charges= '€ '+ 30;
}
$('#controls h2').text(charges);
drivingLine = new google.maps.Polyline({
path: response.routes[0].overview_path,
strokeColor: "#b00",
strokeOpacity: .75,
strokeWeight: 5
});
drivingLine.setMap(map);
map.fitBounds(response.routes[0].bounds);
}
else {
$('#controls p').addClass('error');
}
});
}
$('input').blur(function(){
drivingRoute(
$('input[name=from]').val(),
$('input[name=to]').val()
);
});
$(function(){
$("#geocomplete").geocomplete({
details: "form",
types: ["address"],
country: 'nl'
});
$('input').blur(function(){
$("#geocomplete").trigger("geocode");
});
});
var map;
var drivingLine;
var directionsService = new google.maps.DirectionsService();
initMap();
$('input[name=to]').val('Schiphol');
$('input[name=from]').trigger('blur');
});
// Autocomplete
var fromText = document.getElementById('geocomplete');
var cityBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(52.379189, 4.899431));
var options = {
bounds: cityBounds,
types: ['geocode'],
componentRestrictions: {country: 'nl'}
};
// google.maps.event.addListener(autocomplete, 'place_changed', function() {
// window.alert("you selected an item from suggestion list");
// });
$( "input" ).change(function() {
document.getElementById("bestemming").focus();
return false;
});
document.getElementById('geocomplete').onkeypress = function (e) {
if (e.which === 13) {
document.getElementById("bestemming").focus();
return false;
}
};
例如:
试着把“Damrak,阿姆斯特丹,荷兰”放在出发区,然后点击地图。它显示了次定位中心的价格
将目的地改为“Ceintuurban,Amsterdam,Nederland”后,价格不会改变,只是在点击地图后
我想在填写目的地后立即显示价格,如果用户更改了目的地。价格必须自动更改。不要使用get
blur
事件,请在输入上尝试change
事件
//This is what you have
$('input').blur(function(){
drivingRoute(
$('input[name=from]').val(),
$('input[name=to]').val()
);
});
//Remove/modify the code above and try this
$('#geocomplete').bind('input', function() {
drivingRoute(
$('input[name=from]').val(),
$('input[name=to]').val()
);
});
不完全确定这是否回答了您的问题。我尝试了这两个地方,然后单击了“自动完成”下拉项,它成功了。地图和价格都会自动更新。我不必点击地图来改变它。你是说你想在用户不从下拉列表中选择地点的情况下更新它吗?@搜索你能在不从建议/下拉列表中选择的情况下尝试吗?如果您不从下拉列表中选择,它不会更新价格。不,它不起作用,首先它会显示else价格,单击地图后,它会显示正确子定位的正确价格。。。