首页 > 解决方案 > ASP.NET MVC 引导日期选择器错误

问题描述

我在某些视图上为日期字段实施日期选择器时遇到了一些麻烦。我收到一个错误:

JavaScript 运行时错误:对象不支持属性或方法“日期选择器”

这是我的 _Layout 代码,我似乎无法弄清楚为什么这个错误会继续弹出。任何帮助将不胜感激。

<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

    <script src="@Url.Content("~/Scripts/jquery-2.1.3.js")"
            type="text/javascript"></script>\
    <script src="@Url.Content("~/Scripts/bootstrap.js")"
            type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/boostrap-datepicker.min.js")"
            type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/DatePickerReady.js")"
            type="text/javascript"></script> 

    <script>
        $(function () {
            $('.datepicker').datepicker({
                format: 'mm-dd-yyyy'
            });
        });
    </script>


</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                </ul>
                <p class="nav navbar-text navbar-right">Hello, @User.Identity.Name!</p>
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

标签: asp.net-mvcbootstrap-datepicker

解决方案


问题可能是您的代码通过调用两次添加 jQuery 和 Bootstrap:

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")

这是 jQuery 问题的记录原因。两次添加 jQuery 时,我在此代码段中遇到类似的错误。

<html>
<head>
    <title>DatePicker Example</title>
    <link href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" />
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script>
        $(function () {
            $("#datep").datepicker();
        });
    </script>
</head>
<body>
    <h3>Click to select a date :</h3>
    <input type="text" id="datep" />
    <script src="https://code.jquery.com/jquery-1.12.4.js">
</body>
</html>

未捕获的类型错误:$(...).datepicker 不是函数

只需删除额外的 jQuery 和任何其他重复的库。还包括用户 Shyju 建议的 jQuery UI。

<html>
<head>
    <title>DatePicker Example</title>
    <link href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" />
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script>
        $(function () {
            $("#datep").datepicker();
        });
    </script>
</head>
<body>
    <h3>Click to select a date :</h3>
    <input type="text" id="datep" />
</body>
</html>


推荐阅读