未捕获的TypeError:无法将属性'value'设置为null

小牛

我不断在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'
};

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

因此document.getElementById(component)返回null,并且document.getElementById(component).value使你参考性质valuenull,这将导致错误。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Javascript:未捕获的TypeError:无法将属性'value'设置为null

来自分类Dev

未捕获的Typeerror:无法将属性innerHTML设置为null

来自分类Dev

未捕获的TypeError:无法将属性'type'设置为null

来自分类Dev

未捕获的TypeError:无法将属性X设置为null

来自分类Dev

未被捕获的TypeError:即使属性'value'不为null,也无法将其设置为null

来自分类Dev

未被捕获的TypeError:即使属性'value'不为null,也无法将其设置为null

来自分类Dev

分页:未捕获的TypeError:无法将属性'className'设置为null

来自分类Dev

Javascript:未捕获的TypeError:无法将属性'innerHTML'设置为null

来自分类Dev

错误:未捕获的TypeError:无法将属性'innerHTML'设置为null

来自分类Dev

错误:未捕获(承诺):TypeError:无法将属性“ isAdmin”设置为null

来自分类Dev

通过ID获取元素----未捕获的TypeError:无法将属性'innerHTML'设置为null

来自分类Dev

未捕获的TypeError:无法将属性'innerHTML'设置为null:javascript

来自分类Dev

Iframe输入具有未捕获的TypeError:无法将属性“值”设置为null

来自分类Dev

未捕获到的TypeError:将innerhtml设置为数组中的字符串时,无法设置null的属性“ innerHTML”

来自分类Dev

Javascript onload无法正常工作(“未捕获的TypeError:无法将属性'document.body'设置为null”)

来自分类Dev

未捕获(承诺)TypeError:无法将属性'innerHTML'设置为null-我可以在console.log()中看到值

来自分类Dev

HTML和JavaScript问题“未捕获的TypeError:无法将属性'textContent'设置为null”在控制台中找到

来自分类Dev

未捕获的TypeError:无法设置属性“ innerHTML”

来自分类Dev

未捕获的TypeError:无法在第34行中将属性'innerHTML'设置为null

来自分类Dev

未捕获的TypeError:无法读取null的属性“ on”

来自分类Dev

未捕获的TypeError:无法读取null的属性

来自分类Dev

未捕获的TypeError:无法设置null的属性'innerHTML':(

来自分类Dev

未被捕获的TypeError:无法将属性'innerHTML'设置为null-Typewriter JS Code

来自分类Dev

错误:未被捕获的TypeError:无法将属性'innerHTML'设置为null

来自分类Dev

@Value 将静态变量设置为 null

来自分类Dev

将React升级到0.13.2会导致:“未捕获的TypeError:无法读取null的属性'_currentElement'”

来自分类Dev

尝试将focus()调用到元素ID时收到错误消息:“未捕获的TypeError:无法读取属性'focus'为null”

来自分类Dev

未捕获的TypeError:无法在Android中设置属性“值”>“空”

来自分类Dev

未捕获的TypeError:无法设置未定义的属性“ onClick”

Related 相关文章

  1. 1

    Javascript:未捕获的TypeError:无法将属性'value'设置为null

  2. 2

    未捕获的Typeerror:无法将属性innerHTML设置为null

  3. 3

    未捕获的TypeError:无法将属性'type'设置为null

  4. 4

    未捕获的TypeError:无法将属性X设置为null

  5. 5

    未被捕获的TypeError:即使属性'value'不为null,也无法将其设置为null

  6. 6

    未被捕获的TypeError:即使属性'value'不为null,也无法将其设置为null

  7. 7

    分页:未捕获的TypeError:无法将属性'className'设置为null

  8. 8

    Javascript:未捕获的TypeError:无法将属性'innerHTML'设置为null

  9. 9

    错误:未捕获的TypeError:无法将属性'innerHTML'设置为null

  10. 10

    错误:未捕获(承诺):TypeError:无法将属性“ isAdmin”设置为null

  11. 11

    通过ID获取元素----未捕获的TypeError:无法将属性'innerHTML'设置为null

  12. 12

    未捕获的TypeError:无法将属性'innerHTML'设置为null:javascript

  13. 13

    Iframe输入具有未捕获的TypeError:无法将属性“值”设置为null

  14. 14

    未捕获到的TypeError:将innerhtml设置为数组中的字符串时,无法设置null的属性“ innerHTML”

  15. 15

    Javascript onload无法正常工作(“未捕获的TypeError:无法将属性'document.body'设置为null”)

  16. 16

    未捕获(承诺)TypeError:无法将属性'innerHTML'设置为null-我可以在console.log()中看到值

  17. 17

    HTML和JavaScript问题“未捕获的TypeError:无法将属性'textContent'设置为null”在控制台中找到

  18. 18

    未捕获的TypeError:无法设置属性“ innerHTML”

  19. 19

    未捕获的TypeError:无法在第34行中将属性'innerHTML'设置为null

  20. 20

    未捕获的TypeError:无法读取null的属性“ on”

  21. 21

    未捕获的TypeError:无法读取null的属性

  22. 22

    未捕获的TypeError:无法设置null的属性'innerHTML':(

  23. 23

    未被捕获的TypeError:无法将属性'innerHTML'设置为null-Typewriter JS Code

  24. 24

    错误:未被捕获的TypeError:无法将属性'innerHTML'设置为null

  25. 25

    @Value 将静态变量设置为 null

  26. 26

    将React升级到0.13.2会导致:“未捕获的TypeError:无法读取null的属性'_currentElement'”

  27. 27

    尝试将focus()调用到元素ID时收到错误消息:“未捕获的TypeError:无法读取属性'focus'为null”

  28. 28

    未捕获的TypeError:无法在Android中设置属性“值”>“空”

  29. 29

    未捕获的TypeError:无法设置未定义的属性“ onClick”

热门标签

归档