如何检查字段是否已动态填充

How to check if a field has been populated dynamically?

本文关键字:是否 动态 填充 字段 何检查 检查      更新时间:2023-09-26

我用谷歌地图做了一个计算器,显示出发地和目的地之间的固定价格。

但是,只有在留下输入并单击地图或div后,价格才会显示。

    $(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;
        }
    };

示例:http://codepen.io/anon/pen/rMEdKO/

尝试将"Damrak, Amsterdam, Nederland"放在出发字段上,然后点击地图。它显示了sublocality Centrum的价格。

将目的地改为"cetuurbaan, Amsterdam, Nederland"后,价格不会改变,只有在点击地图后才会改变。

我想在填写目的地后显示价格,如果用户更改了目的地。价格必须自动调整

不是使用get blur事件,而是在input上尝试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()
        );
    });

不完全确定这是否回答了你的问题。