【问题标题】:jQuery: Can't call a function [duplicate]jQuery:无法调用函数[重复]
【发布时间】:2014-03-07 20:13:19
【问题描述】:

我有一些使用 Telerik RadGrid 的代码。当我把它放在一个新页面上时,我一直得到 $ is undefined ajax is undefined 直到我引入了 jQuery 库(即使它包含在母版页中)。我试图在按下按钮时调用 DoUpdate 并得到未定义的错误。

 <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
    <script src="../Scripts/jquery-1.10.2.min.js"></script> 
    <script type="text/javascript">
        $(document).ready(function () {

            function DoUpdate(sbiId) {
                var input = '{"SbiId":"' + sbiId + '"}';
                var dataSource;

                $.ajax({
                    url: "http://www.blah.com/services/testsService.svc/GetContactsDataAndCount",
                    type: "GET",
                    contentType: "application/json; charset=utf-8",
                    data: input,
                    dataType: "json",
                    success: function (data) {
                        // updateGrid(data);
                        var mtv = $find("<%= RadGrid1.ClientID %>").get_masterTableView();
                        console.log(data);
                        mtv.set_dataSource(data.d.Data);
                        mtv.dataBind();
                    }
                });
                }

        });
        </script>
    </telerik:RadCodeBlock>

    <p>
          <asp:Button ID="Button1" runat="server" Text="Update" OnClientClick="DoUpdate(1); return false" />
      </p>

【问题讨论】:

  • 您在另一个函数($(document).ready 回调)的 inside 中声明了您的函数。这使它无法访问。在全局范围内声明它,以便您的按钮可以访问它。更好的是,用 JS 绑定你的事件,而不是 OnClientClick。不要使用$(document).ready,除非您需要并了解其用途。

标签: javascript jquery telerik


【解决方案1】:

DoUpdate 在 $(document).ready(function() { }) 内部 - 因此,外部的任何东西都无法访问它。

这里有两种方法可以解决这个问题 - 使用 jQuery 选择按钮并连接 click 事件,或者将该函数暴露在 ready 块之外。这是后者:

 <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
    <script src="../Scripts/jquery-1.10.2.min.js"></script> 
    <script type="text/javascript">
        var DoUpdate;
        $(document).ready(function () {

            DoUpdate = function(sbiId) {
                var input = '{"SbiId":"' + sbiId + '"}';
                var dataSource;

                $.ajax({
                    url: "http://www.blah.com/services/testsService.svc/GetContactsDataAndCount",
                    type: "GET",
                    contentType: "application/json; charset=utf-8",
                    data: input,
                    dataType: "json",
                    success: function (data) {
                        // updateGrid(data);
                        var mtv = $find("<%= RadGrid1.ClientID %>").get_masterTableView();
                        console.log(data);
                        mtv.set_dataSource(data.d.Data);
                        mtv.dataBind();
                    }
                });
                }
        });
        </script>
    </telerik:RadCodeBlock>

    <p>
          <asp:Button ID="Button1" runat="server" Text="Update" OnClientClick="DoUpdate(1); return false" />
      </p>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 2013-04-19
    • 2012-06-11
    • 2018-04-08
    • 1970-01-01
    相关资源
    最近更新 更多