c# - javascript google Places 在 html 中工作,但在 asp 内容中不起作用

标签 c# javascript asp.net forms google-places

我正在尝试使用 javascript 示例,使用 google place api 自动填充一些地址字段。它按计划在 html 中工作,但如果我想在标签内使用它,它就不起作用。我在 Firefox 中没有看到任何 js 错误。我认为我没有正确调用 javascrip。非常感谢您的帮助。我的asp页面、js文件和site.master的代码如下:

test.aspx asp头

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.Master" CodeBehind="test.aspx.cs" Inherits="Project.test" %>

test.aspx页面

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent" onload="javascript:initialize()">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true&libraries=places"></script>
<script type="text/javascript" src="../Scripts/auto_complete.js"></script>

<div id="locationField">

</div>
  <div id="consent-form-container">

  <div class="box">
     <table>
        <tr>
          <td>
            <asp:Label ID="Label8" runat="server" Text="Type Your Address:"></asp:Label>
          </td>
          <td>
            <asp:TextBox id="autocomplete" type="text" runat="server" placeholder="Enter your address" onFocus="javascript:geolocate()"></asp:TextBox>
          </td>
        </tr>

        <tr>
            <td>
              <asp:Label ID="Label9" runat="server" Text="Apt/Suite:"></asp:Label>
            </td>
            <td>
              <asp:TextBox ID="apt_suite1" runat="server" type="text" CssClass="apt_suite"></asp:TextBox>
            </td>
       </tr>                
    </table>
  </div>

  <div class="box">
  <table id="address">
    <tr>
      <td class="label">Street address</td>
      <td class="slimField"><asp:TextBox class="field" runat="server" id="street_number" type="text" disabled="true"></asp:TextBox></td>
      <td class="wideField" colspan="2"><input class="field" id="route" type="text" disabled="true"></input></td>
    </tr>
    <tr>
      <td class="label">City</td>
      <td class="wideField" colspan="3"><asp:TextBox class="field" runat="server" id="locality" type="text" disabled="true"></asp:TextBox></td>
    </tr>
    <tr>
      <td class="label">State</td>
      <td class="slimField"><asp:TextBox class="field" runat="server" id="administrative_area_level_1" type="text" disabled="true"></asp:TextBox></td>
    </tr>
    <tr>
      <td class="label">Zip code</td>
      <td class="wideField"><asp:TextBox class="field" runat="server" id="postal_code" type="text" disabled="true"></asp:TextBox></td>
    </tr>
  </table>
  </div>
  </div>
  </asp:Content>

auto_complete.js

var placeSearch, autocomplete;
var component_form = {
    'street_number': 'short_name',
    'route': 'long_name',
    'locality': 'long_name',
    'administrative_area_level_1': 'short_name',
    'postal_code': 'short_name'
};

function initialize() {
    autocomplete = new google.maps.places.Autocomplete(document.getElementById('<%= autocomplete.ClientID %>'), { types: ['geocode'] });
    google.maps.event.addListener(autocomplete, 'place_changed', function () {
        fillInAddress();
    });
}

function fillInAddress() {
    var place = autocomplete.getPlace();

    for (var component in component_form) {
        document.getElementById(component).value = "";
        document.getElementById(component).disabled = false;
    }

    for (var j = 0; j < place.address_components.length; j++) {
        var att = place.address_components[j].types[0];
        if (component_form[att]) {
            var val = place.address_components[j][component_form[att]];
            document.getElementById(att).value = val;
        }
    }
}

 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));
        });
    }
}

和site.Master

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Project.SiteMaster" %>

<!DOCTYPE html>
<html lang="es">
<head id="head" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta charset="utf-8" />
<title><%: Page.Title %> title </title>
<asp:PlaceHolder runat="server">     
      <%: Scripts.Render("~/bundles/modernizr") %>
</asp:PlaceHolder>  
<webopt:BundleReference runat="server" Path="~/Content/css" /> 
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
<asp:ContentPlaceHolder runat="server" ID="HeadContent" />
</head>
<body id="body" runat="server">
    <form runat="server">
    <asp:ScriptManager runat="server">
    <Scripts>
        <%--Site Scripts--%>

    </Scripts>
</asp:ScriptManager>
<header>
    <div class="content-wrapper">
        <div class="float-left">
            <p class="site-title">
                <a runat="server" href="~/"><img src="/Images/logo.png" /></a>
            </p>
        </div>  
        <div class="float-right">
            <section id="login">
                <asp:LoginView runat="server" ViewStateMode="Disabled">
                    <AnonymousTemplate>
                        <ul>
                            <li><a id="loginLink" runat="server" href="~/Account/Login">Login</a></li>
                        </ul>
                    </AnonymousTemplate>
                    <LoggedInTemplate>
                        <p>
                            Welcome admin!, <a runat="server" class="username" href="~/admin" title="" style="color:white">
                                <!--<asp:LoginName runat="server" CssClass="username" ForeColor="White" />-->View Forms</a>
                            <asp:LoginStatus runat="server" LogoutAction="Redirect" LogoutText="Logout" LogoutPageUrl="~/" />
                        </p>
                    </LoggedInTemplate>
                </asp:LoginView>
            </section>
        </div>
    </div>
</header>
<div id="body_div">
    <asp:ContentPlaceHolder runat="server" ID="FeaturedContent" />
    <section class="content-wrapper main-content clear-fix">
        <asp:ContentPlaceHolder runat="server" ID="MainContent" />
    </section>
</div>
<footer>
    <div class="content-wrapper">
        <div class="float-left">
            <p>&copy; <%: DateTime.Now.Year %> - Riviera Tanning Spa</p>
        </div>
    </div>
</footer>
</form>
</body>
</html>

最佳答案

像这样改变js

document.getElementById("MainContent_autocomplete")

关于c# - javascript google Places 在 html 中工作,但在 asp 内容中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16488764/

相关文章:

c# - 我的网站在没有 PDB 文件的情况下崩溃

c# - 循环静态只读字段定义的奇怪行为

javascript - 帮助文件和外部函数的 Jest 测试

javascript - 通过 Javascript 将 IFRAME 添加到 DOM

asp.net - S#arp 是否适用于经典 ASP.NET 甚至 WinForms?

c# - 带有 NInject 的 API 应用程序不适用于 IIS,它仅适用于调试器 Visual Studio

c# - Asp.net 下拉列表值在 jquery 对话框中不会更改

javascript - 我应该在 Node.js 中使用哪种客户端模板?

javascript - 无穷大与 Number.POSITIVE_INFINITY

asp.net - 如何从 FormCollection 中获取复选框值?