我怎么称呼这种方法?

时间:2013-04-01 17:39:33

标签: javascript asp.net xml html5

 <%@ 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()函数,但它不起作用。这段代码没问题。问题是调用函数是错误的。所以,请你能告诉我我该怎么做..

4 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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" />

答案 2 :(得分: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" />

答案 3 :(得分:0)

为什么不直接调用该函数?

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

否则,在Button1_onclick中应该返回一个值。

function Button1_onclick() {
        calcRoute();
        return true;
}