简体   繁体   中英

ReferenceError: $ is not defined

This is my code which does the functionality of auto address fill up and identifying the chosen latitude/longitude/address.

<html>

<head>
<LINK rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
</head>

<body>
<p>Location: <input type="text" id="searchTextField" size="50"  placeholder="Where do you want to go ?"/></p>

<div id="results"></div>

<script>


var input = document.getElementById('searchTextField');
var options = {

};
var autocomplete = new google.maps.places.Autocomplete(input,options);
//autocomplete.bindTo('bounds', map); 

google.maps.event.addListener(autocomplete, 'place_changed', function() {
     $("#results").html('');
  var place = autocomplete.getPlace();
    $("#results").append('<p> Latitude and Longtidute : '+place.geometry.location +'</p>');
    $("#results").append('<p> Address : '+place.formatted_address +'</p>');
    $("#results").append('<p> Places Name : '+place.name+'</p>');

    var searchAddressComponents = place.address_components;
    $.each(searchAddressComponents, function(){
    if(this.types[0]=="postal_code"){
        searchCountry=this.short_name;
    } 
});
});
</script>
</body>
</html>

i thought everything is correct but it throws this error in firebug ReferenceError: $ is not defined

can you please help me with it

please check http://jsfiddle.net/NuCUd/8/ for reference

$ is defined by jQuery, which is an ordinary, 3rd-party library.

If you don't include it, it won't exist.

Change your code to:

<html>

<head>
<LINK rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
</head>

<body>
<p>Location: <input type="text" id="searchTextField" size="50"  placeholder="Where do you want to go ?"/></p>

<div id="results"></div>

<script>


var input = document.getElementById('searchTextField');
var options = {

};
var autocomplete = new google.maps.places.Autocomplete(input,options);
//autocomplete.bindTo('bounds', map); 

google.maps.event.addListener(autocomplete, 'place_changed', function() {
     $("#results").html('');
  var place = autocomplete.getPlace();
    $("#results").append('<p> Latitude and Longtidute : '+place.geometry.location +'</p>');
    $("#results").append('<p> Address : '+place.formatted_address +'</p>');
    $("#results").append('<p> Places Name : '+place.name+'</p>');

    var searchAddressComponents = place.address_components;
    $.each(searchAddressComponents, function(){
    if(this.types[0]=="postal_code"){
        searchCountry=this.short_name;
    } 
});
});
</script>
</body>
</html>

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