ReferenceError: $ is not defined

假如想象 提交于 2019-12-20 07:25:52

问题


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


回答1:


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>



回答2:


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

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



来源:https://stackoverflow.com/questions/18538860/referenceerror-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!