简体   繁体   中英

Uncaught TypeError: Cannot read property 'cities' of undefined [Geocomplete]

I am using this plugin in Rails but still have a problem with error in title.

Definition of script:

script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=MY_API_KEY&libraries=places&sensor=false>

Input text field:

<%= f.text_field :place, id: "cities", class: "form-control cities", type: "text" %>

Javascript load:

$("cities").geocomplete();
$.fn.geocomplete("cities"); 

I have done everything as described on GitHub but still cannot get it works. If I use $("#cities").geocomplete(); the error is the same.

Please, can somebody help me?

You should also add the jquery.geocomplete.js script inorder to make the geo-complete work.

Scripts:

<script src="http://maps.googleapis.com/maps/api/js?libraries=places"></script>
<script src="jquery.geocomplete.js"></script>

You should load these two scripts, then call the function.

$("#cities").geocomplete();  // Option 1: Call on element.

Here is the reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM