简体   繁体   English

jQuery-未捕获ReferenceError:未定义geolocate

[英]JQuery - Uncaught ReferenceError: geolocate is not defined

Trying to use the Google Address auto-complete API. 尝试使用Google地址自动完成API。 My page loads fine however when initiating a search it doesn't return anything. 我的页面加载正常,但是在启动搜索时未返回任何内容。 When I Inspect Element I get the following error [Uncaught ReferenceError: geolocate is not defined]. 当我检查元素时,出现以下错误[Uncaught ReferenceError:未定义geolocate]。 I've searched resources on SOF and have tried the following: 我在SOF上搜索了资源,并尝试了以下方法:

  • I'v moved my script to the bottom of the page 我已将脚本移到页面底部
  • I've Added the $(document).ready(function () 我已经添加了$(document).ready(function()
  • Declared all my var globally 全局声明我所有的变量
  • Verified all referenced id's exists 验证所有引用的ID存在

I would really appreciate so help. 我非常感谢您的帮助。 This has me pulling out what little hair I have left. 这使我拔出了剩下的小头发。 Thanks 谢谢

Here is my code: 这是我的代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"     CodeFile="tet.aspx.cs" Inherits="tet" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="Server">

<link href="Content/bootstrap.min.css" rel="stylesheet" />
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="Scripts/bootstrap.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500">
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>

<div class="container-fluid">

    <%--   <asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>--%>
    <asp:Panel ID="Panel1" runat="server">
        <br />

        <div class="form-group">
            <div class="row">
                <div class="col-md-8">
                    <asp:TextBox ID="autocomplete" CssClass="form-control input-lg" runat="server" onFocus="geolocate()" placeholder="Enter your address..."></asp:TextBox>
                    <asp:HiddenField ID="street_number" runat="server" />
                    <asp:HiddenField ID="route" runat="server" />
                </div>
            </div>
            <br />

            <div class="row">
                <div class="col-md-8">
                    Address:
<asp:TextBox ID="tb_AddressLine1" CssClass="form-control" ReadOnly="true" runat="server" ClientIDMode="Static"></asp:TextBox>
                </div>
            </div>
            <br />

            <div class="row">
                <div class="col-md-4">
                    City:
<asp:TextBox ID="locality" runat="server" CssClass="form-control" ReadOnly="true"></asp:TextBox>
                </div>
                <div class="col-md-2">
                    State:
<asp:TextBox ID="administrative_area_level_1" runat="server" CssClass="form-control" ReadOnly="true"></asp:TextBox>
                </div>
                <div class="col-md-2">
                    Postal Code:
<asp:TextBox ID="postal_code" runat="server" CssClass="form-control" ReadOnly="true"></asp:TextBox>
                </div>
                <div class="col-md-3">
                    Country:
<asp:TextBox ID="country" runat="server" CssClass="form-control" ReadOnly="true"></asp:TextBox>
                </div>
            </div>
        </div>


<br />
</asp:Panel>
--%> - %>
</div</asp:Content>

 </div</asp:Content> 

I figured it out... I forgot to add ClientIDMode="Static" to all TextBoxes, so when the page is loaded the JavaScript doesn't "see" the fields it is looking for. 我想通了...我忘记将ClientIDMode =“ Static”添加到所有TextBoxes中,因此,在加载页面时,JavaScript不会“看到”它要查找的字段。 Added that and it worked like a charm. 补充说,它就像一个魅力。

Instead of doing this on focus of the textbox, you can just activate it by calling geolocate() after having initialized the searchbox. 不必着眼于文本框,而只需在初始化搜索框后调用geolocate()即可激活它。 This will surely work. 这肯定会起作用。

I mistakenly assumed that you were using a Google Map originally. 我错误地认为您最初使用的是Google地图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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