为什么我会收到“未捕获的引用错误:$未定义(匿名函数)”

发布于 2024-12-23 18:57:06 字数 2257 浏览 1 评论 0原文

我有一个 cshtml 页面,出现以下错误:

Uncaught ReferenceError: $ is not defined               TabNotes:2
(anonymous function)                                    TabNotes:2
TabNotes:12Uncaught ReferenceError: ko is not defined   TabNotes:12
(anonymous function)                                    TabNotes:12
(anonymous function)                                    TabNotes:23

What can Cause such an error?我找不到任何原因。我尝试将 javascript 函数包装在 $(document).ready(function () { 中,但这也不起作用。代码如下

@model test.Web.Framework.Areas.Administration.Models.TabNotesModel 
@using (UI.DocumentReadyScript())
{    
    if (Model.meta.id.HasValue)
    {
        UI.jQuery("#tbl" + Model.meta.modelname).flexigrid(Model.Grid);
    }
}
<form method="post" action="@Url.Action("TabNotes", new { cmd = "refresh" })" id="@Model.meta.modelname">
<div class="ui-state-highlight ui-corner-all highlight" data-bind="visible: meta.message">
    <span class="ui-icon ui-icon-info"></span><strong data-bind="text: meta.message">
    </strong>
</div>
@using (UI.BeginBlock("Administation.TabNotes", UI.Label("Notes", "Notes").ToString(), test.Web.Framework.Core.enumIcons.pencil, false, false))
{
    <table id="@("tbl" + Model.meta.modelname)">
    </table>
} 
</form>
<script type="text/javascript">
(function() {
        var viewModel=ko.mapping.fromJS(@Html.Raw(UI.JavascriptEncode(Model)));
        viewModel.getData=function() { return ko.mapping.toJSON( this  ); };   
        viewModel.setData=function(data){ 
        $('#tbl'+this.meta.modelname()).flexigrid( data.Grid);
        ko.mapping.updateFromJS(this,data); 
        };
        $('#@Model.meta.modelname').koform({viewmodel: viewModel , validate : {errorElement:'p' }  } );
        $('#@Model.meta.modelname').koform('applyBindings');
        $('#load-partial').click(function() {
            $('#partial').load('@Html.Raw(@Url.Action("CreateNote", "Entity", new {modelEntity = @Model.meta.entity, itemId = @Model.meta.id}))');
        }); 
    })(); 
</script>


<div id="partial"></div>
<button type="button" id="load-partial">Create Note</button>

编辑: jQuery 从母版页加载。

I have a cshtml page that gives these errors:

Uncaught ReferenceError: $ is not defined               TabNotes:2
(anonymous function)                                    TabNotes:2
TabNotes:12Uncaught ReferenceError: ko is not defined   TabNotes:12
(anonymous function)                                    TabNotes:12
(anonymous function)                                    TabNotes:23

What can cause such an error? I can't find any reason why this would be. I tried wrapping the javascript function in $(document).ready(function () { but that did not work either. The code is below

@model test.Web.Framework.Areas.Administration.Models.TabNotesModel 
@using (UI.DocumentReadyScript())
{    
    if (Model.meta.id.HasValue)
    {
        UI.jQuery("#tbl" + Model.meta.modelname).flexigrid(Model.Grid);
    }
}
<form method="post" action="@Url.Action("TabNotes", new { cmd = "refresh" })" id="@Model.meta.modelname">
<div class="ui-state-highlight ui-corner-all highlight" data-bind="visible: meta.message">
    <span class="ui-icon ui-icon-info"></span><strong data-bind="text: meta.message">
    </strong>
</div>
@using (UI.BeginBlock("Administation.TabNotes", UI.Label("Notes", "Notes").ToString(), test.Web.Framework.Core.enumIcons.pencil, false, false))
{
    <table id="@("tbl" + Model.meta.modelname)">
    </table>
} 
</form>
<script type="text/javascript">
(function() {
        var viewModel=ko.mapping.fromJS(@Html.Raw(UI.JavascriptEncode(Model)));
        viewModel.getData=function() { return ko.mapping.toJSON( this  ); };   
        viewModel.setData=function(data){ 
        $('#tbl'+this.meta.modelname()).flexigrid( data.Grid);
        ko.mapping.updateFromJS(this,data); 
        };
        $('#@Model.meta.modelname').koform({viewmodel: viewModel , validate : {errorElement:'p' }  } );
        $('#@Model.meta.modelname').koform('applyBindings');
        $('#load-partial').click(function() {
            $('#partial').load('@Html.Raw(@Url.Action("CreateNote", "Entity", new {modelEntity = @Model.meta.entity, itemId = @Model.meta.id}))');
        }); 
    })(); 
</script>


<div id="partial"></div>
<button type="button" id="load-partial">Create Note</button>

edit:
The jQuery gets loaded from the master page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冧九 2024-12-30 18:57:06

这意味着 jQuery ($) 和 knockout (ko) 未定义。当执行脚本时库尚未加载时,通常会出现这种情况。

确保在使用框架之前加载它们(即,与框架相关的脚本标签插入在页面级脚本之前)。如果效果良好,请检查开发人员面板中的网络活动,看看下载框架时是否收到错误的请求。您可能正在异步加载框架,这意味着您需要一个异步处理程序来开始执行页面级脚本。

It means that jQuery ($) and knockout (ko) are not defined. This often comes up when a library isn't loaded by the time the script has been executed.

Ensure that you're loading the frameworks prior to using them (i.e. your framework-related script tags are inserted prior to your page-level scripts). If that's good, check your network activity in your developer panel to see if you're getting erroneous requests when downloading the frameworks. It could be that you're loading the frameworks asynchronously, which means you'll need an async handler to begin executing your page-level script.

暮年 2024-12-30 18:57:06

您在代码中的何处引用了 KO 和 jQuery?通常会出现此错误,因为您的代码尝试在加载引用之前运行。

我建议使用 Fiddler 或其他网络活动工具来查看引用何时加载(或未加载)以及来自何处。

Where in your code are you referencing KO and jQuery? Usually this error occurs because your code tries to run before the references are loaded.

I recommend using Fiddler or another network activity tool to watch when the references are loaded (or not) and where from.

烛影斜 2024-12-30 18:57:06

当我尝试将 jquery ui 小部件合并到我的网站时,我遇到了此错误的问题。我必须更新所包含的 css 文件的正确路径;有时可能会很挑剔。也必须对 jquery 做同样的事情。另请注意您的本地主机与“src=”/code/blah_folder”所在的位置。希望有帮助。

I had problems with this error when trying to incorporate a jquery ui widget into my site. I had to update the proper path of the css file being included; it can be picky sometimes. Also had to do the same for the jquery as well. Also take note where your localhost is with regards to where your 'src="/code/blah_folder" is. Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文