javascript - 未捕获的类型错误 : Cannot set property 'value' of null

标签 javascript

我在 fillInAddress 函数上不断收到该错误。

这是我的表单 html:

<div id="locationField">
  <input id="autocomplete" placeholder="Enter your address" onFocus="geolocate()" type="text" width="500px"></input>
</div>

<form accept-charset="UTF-8" action="/jobs" class="new_job" id="new_job" method="post">
  <div class="field">
    <label for="job_address">Customer</label><br>
    <input id="customer" name="job[address]" type="text" />
  </div>
  <div class="field">
    <label for="job_address">Address</label><br>
    <input disabled="disabled" id="street_name" name="job[address]" type="text" />
  </div>
  <div class="field">
    <label for="job_route">Route</label><br>
    <input disabled="disabled" id="route" name="job[route]" type="text" />
  </div>
  <div class="field">
    <label for="job_city">City</label><br>
    <input disabled="disabled" id="locality" name="job[city]" type="text" />
  </div>
  <div class="field">
    <label for="job_state">State</label><br>
    <input disabled="disabled" id="administrative_area_level_1" name="job[state]" type="text" />
  </div>
  <div class="field">
    <label for="job_postal_code">Postal code</label><br>
    <input disabled="disabled" id="postal_code" name="job[postal_code]" type="text" />
  </div>
  <div class="field">
    <label for="job_country">Country</label><br>
    <input disabled="disabled" id="country" name="job[country]" type="text" />
  </div>
  <div class="actions">
    <input name="commit" type="submit" value="Create Job" />
  </div>
</form>

还有我的 JS:

    <script>
// This example displays an address form, using the autocomplete feature
// of the Google Places API to help users fill in the information.

var placeSearch, autocomplete;
var componentForm = {
  street_number: 'short_name',
  route: 'long_name',
  locality: 'long_name',
  administrative_area_level_1: 'short_name',
  country: 'long_name',
  postal_code: 'short_name'
};

function initialize() {
  // Create the autocomplete object, restricting the search
  // to geographical location types.
  autocomplete = new google.maps.places.Autocomplete(
      /** @type {HTMLInputElement} */(document.getElementById('autocomplete')),
      { types: ['geocode'] });
  // When the user selects an address from the dropdown,
  // populate the address fields in the form.
  google.maps.event.addListener(autocomplete, 'place_changed', function() {
    fillInAddress();
  });
}

// [START region_fillform]
function fillInAddress() {
  // Get the place details from the autocomplete object.
  var place = autocomplete.getPlace();

  for (var component in componentForm) {
    document.getElementById(component).value = '';
    document.getElementById(component).disabled = false;
  }

  // Get each component of the address from the place details
  // and fill the corresponding field on the form.
  for (var i = 0; i < place.address_components.length; i++) {
    var addressType = place.address_components[i].types[0];
    if (componentForm[addressType]) {
      var val = place.address_components[i][componentForm[addressType]];
      document.getElementById(addressType).value = val;
    }
  }
}
// [END region_fillform]

// [START region_geolocation]
// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var geolocation = new google.maps.LatLng(
          position.coords.latitude, position.coords.longitude);
      autocomplete.setBounds(new google.maps.LatLngBounds(geolocation,
          geolocation));
    });
  }
}
// [END region_geolocation]

    </script>

当我从自动完成输入字段中选择结果时,应该填充表单字段。然而,这并没有发生。当我直接从这里剪切并粘贴代码时 https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform ,效果很好。

最佳答案

var componentForm = {
  street_number: 'short_name',
  route: 'long_name',
  locality: 'long_name',
  administrative_area_level_1: 'short_name',
  country: 'long_name',
  postal_code: 'short_name'
};

componentForm 的第一个属性是 street_number。您的 HTML 中没有任何 ID 为 street_number 的元素。

因此 document.getElementById(component) 返回 null,并且 document.getElementById(component).value 使您引用属性 value null,这会导致错误。

根据您的表单,该属性的正确名称应该是 street_name

关于javascript - 未捕获的类型错误 : Cannot set property 'value' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22776473/

相关文章:

javascript - 无需 cookie 或本地存储的用户识别

javascript - 如何使用 Jest 实现共享测试用例?

javascript - 如何正确重定向所选列表

javascript - ion-nav-view 中的 ng-style 或 style 没有从 Controller 获得值(value)

javascript - 使浏览器窗口在任务栏中闪烁

javascript - jQuery javascript 作用域问题

javascript - 如何检查路径是绝对路径还是相对路径

javascript - 如何创建一个简单的启动画面/页面?

javascript - 谷歌地图移除默认人形图标

javascript - 两种方式隔离绑定(bind) Angular Directive(指令) es6