2013-04-01 41 views
0
<%@ Page Title="About" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="About.aspx.cs" Inherits="About" %> 

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent"> 
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
    <script type="text/javascript"> 
    var directionsDisplay; 
    var directionsService = new google.maps.DirectionsService(); 


    function getParameterByName(name) { 
     name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); 
     var regexS = "[\\?&]" + name + "=([^&#]*)"; 
     var regex = new RegExp(regexS); 
     var results = regex.exec(window.location.search); 
     if (results == null) 
      return ""; 
     else 
      return decodeURIComponent(results[1].replace(/\+/g, " ")); 
    } 


    function calcRoute() { 

     start = document.getElementById('startvalue').value; 

     end = document.getElementById('endvalue').value; 

     var request = { 
      origin: start, 
      destination: end, 
      travelMode: google.maps.DirectionsTravelMode.DRIVING 
     }; 

     directionsService.route(request, function (response, status) { 
      if (status == google.maps.DirectionsStatus.OK) { 


       directionsDisplay.setDirections(response); 
       this.distance = response.routes[0].legs[0].distance.value/1000; 

      } 
     }); 

    } 



    function Button1_onclick() { 
     calcRoute(); 
    } 

    window.onload = InitializeMap; 
    </script> 


    <div style="height: 86px; width: 689px; margin-left: 16px; margin-top: 0px"> 

     <table style="width: 96%;"> 
      <tr> 
       <td class="auto-style1"> 
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
       </td> 
       <td> 
        <asp:TextBox runat="server" id ="startvalue"></asp:TextBox> 
       </td> 
       <td>&nbsp;</td> 
      </tr> 
      <tr> 
       <td class="auto-style2"> 
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> 
       </td> 
       <td class="auto-style3"> 
        <asp:TextBox runat="server" id ="endvalue"></asp:TextBox> 
       </td> 
       <td class="auto-style3"></td> 
      </tr> 
      <tr> 
       <td class="auto-style1">&nbsp;</td> 

       <td>     
         <input id="Button1" type="button" value="GetDirections" onclick="return Button1_onclick()" /> 

      </tr> 

     </table> 

我想調用Button1_onclick()函數,但它不起作用。這段代碼沒有問題。問題是調用函數是錯誤的。所以,請你能告訴我,我該怎麼辦呢..我該如何調用此方法?

回答

0

這可能是因爲您的無效HTML標記

  <tr> 
       <td class="auto-style1">&nbsp;</td> 

       <td>     
         <input id="Button1" type="button" value="GetDirections" onclick="return Button1_onclick()" /> 

</td> <!-- Missing closing tag --> 

      </tr> 
+0

No.that不是問題,仍然不能打電話..如果你能幫助我。 – user2232995

0

的試試這個代碼的onclick =「JavaScript的回報:calcRoute()代替的OnClick =「返回 Button1_onclick」

和檢查這個腳本文件(**<script type="text/javascript" src="../../Scripts/jquery-ui-1.9.2.min.js"></script>**)(任何最小化版本)在您的母版頁闖民宅??

   <tr> 
       <td class="auto-style1">&nbsp;</td> 

       <td>     
        <input id="Button1" type="button" value="GetDirections" *onclick="return javascript:calcRoute();"** /> 
       </td>  
      </tr> 

編輯:

這種嘗試另一種方式

$("#filter").click(function(){ 
    Button1_onclick(); 
}); 

<input id="Button1" type="button" value="GetDirections" /> 
+0

不,這不是問題,仍然不能打電話。如果你能幫助我。 – user2232995

+0

請參閱並嘗試編輯我的代碼 –

0

您是否嘗試去除函數調用的回報?此代碼觸發該事件。

<asp:Content ID="ContentMain" ContentPlaceHolderID="ContentBody" runat="server">  
    <script type="text/javascript" language="javascript"> 
     function Button1_Click() { 
      alert('foo'); 
     } 
    </script> 
<input type="button" id="Button1" onclick="Button1_Click()" value="GetDirections" /> 

0

爲什麼你不只是調用函數呢?

<input id="Button1" type="button" value="GetDirections" onclick="Button1_onclick()" /> 

否則,在Button1_onclick應該返回一個值。

function Button1_onclick() { 
     calcRoute(); 
     return true; 
} 
相關問題