MVC 4 Razor 和 Jquery UI datepicker()“对象不支持属性或方法”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19394403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:51:17  来源:igfitidea点击:

MVC 4 Razor and Jquery UI datepicker() "Object doesn't support property or method"

jqueryasp.net-mvcjquery-uirazordatepicker

提问by Porschiey

I've probably just spent the last two hours trying to figure this out.

我可能只是花了最后两个小时试图弄清楚这一点。

The specific error that is thrown with my MVC application is this: "0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'datepicker'"

我的 MVC 应用程序抛出的具体错误是:“0x800a01b6 - JavaScript 运行时错误:对象不支持属性或方法‘datepicker’”

Here is my code:

这是我的代码:

_Layout.cshtml:

_Layout.cshtml:

<head>

...
<script type="text/javascript" src="../../Scripts/jquery-2.0.3.js"></script>
<script type="text/javascript" src="../../Scripts/jquery-ui-1.10.3.js"></script>
</head>

the view .cshtml

视图 .cshtml

@{
    ...
    Layout = "~/Views/Shared/_Layout.cshtml";
}

...
<div>
    Change Time Span:
    <br />
    From:
    <input type="text" id="test" />
</div>

<div class="loading" id="container">@Model.LoadingMessage</div>

<script type="text/javascript">
    $(document).ready(function () {

        $('#test').datepicker();
    });

</script>

I've looked at several potential "duplicate" questions with no success. Here are some of the questions I've looked at:

我查看了几个潜在的“重复”问题,但没有成功。以下是我看过的一些问题:

MVC3 razor view error Microsoft JScript runtime error: Object doesn't support property or method 'datepicker'`jQuery giving error "Object doesn't support this property or method" on method datepickerUsing datepicker in MVC 4.0 to persist a date value in db, throwing "object doesn't support this property or method"

MVC3 剃刀视图错误 Microsoft JScript 运行时错误:对象不支持属性或方法 'datepicker'` jQuery 在方法 datepicker 上给出错误“对象不支持此属性或方法”在 MVC 4.0 中使用 datepicker 在 db 中保留日期值, 抛出“对象不支持此属性或方法”

  1. I believe I am referencing all that I need from jquery to accomplish my goal.
  2. The element is readable from Jquery, it can see its value were I to set it, so I don't think my selector is the problem.
  3. Both Jquery Packages were installed via NuGet (the latest Jquery release and the Jquery UI Combined Library)
  4. I've rebuilt / cleaned the Visual Studio solution (in desperation), shut down the local IIS host and reset it...
  1. 我相信我正在从 jquery 中引用所有我需要的东西来实现我的目标。
  2. 该元素可以从 Jquery 读取,如果我设置它,它可以看到它的值,所以我认为我的选择器不是问题。
  3. 两个 Jquery 包都是通过 NuGet 安装的(最新的 Jquery 版本和 Jquery UI 组合库)
  4. 我已经重建/清理了 Visual Studio 解决方案(绝望),关闭本地 IIS 主机并重置它......

What am I missing? Intellisense even detects that the method exists and is giving me suggestions for what to place in for parameters.

我错过了什么?Intellisense 甚至检测到该方法存在,并为我提供有关放置参数的建议。

Is there something simple I've missed?

有什么简单的我错过了吗?

采纳答案by kavitha Reddy

Required scripts
----------------

<link href="@Url.Content("~/Content/themes/base/jquery-ui.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.9.1.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui-1.10.0.js")" type="text/javascript"></script>



<p>Date:<input type="text" id="Dob" class="datefield" /></p>



<script type="text/javascript">
        $(document).ready(function () {
            $("#Dob").datepicker({
                changeMonth: true,
                changeYear: true
            });
        });
    </script>

回答by Caffeinius

I found that by commenting out the "@Scripts.Render("~/bundles/jquery")" towards the bottom of _Layout.cshtml, I was able to resolve my issue with this.

我发现通过注释掉 _Layout.cshtml 底部的“@Scripts.Render("~/bundles/jquery")”,我能够解决我的问题。

回答by Eddie

Have you checked you haven't referenced JQuery twice? I found I'd included it in my layout and in my view. Removing one reference solved the problem.

你检查过你没有两次引用 JQuery 吗?我发现我已经将它包含在我的布局和我的视图中。删除一个引用解决了这个问题。

回答by Bashar Abu Shamaa

Try this :

尝试这个 :

     <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

or this :

或这个 :

     <script" src="~/Scripts/jquery-ui-1.10.3.js"></script>

Instead of this :

而不是这个:

   <script type="text/javascript" src="../../Scripts/jquery-ui-1.10.3.js"></script>

回答by shawad

I had this issue when upgrading to jQuery 2. I was caught out by the change in folder structure within the jQuery download; the jquery-ui.js file was no longer located in a ui folder and therefore my script tag was incorrectly mapped.

我在升级到 jQuery 2 时遇到了这个问题。我被 jQuery 下载中文件夹结构的变化所吸引;jquery-ui.js 文件不再位于 ui 文件夹中,因此我的脚本标记被错误地映射。

So in short, double check your references are correct!

简而言之,请仔细检查您的参考资料是否正确!