asp.net-mvc - 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>© @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>
解决方案
问题可能是您的代码通过调用两次添加 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>
推荐阅读
- php - 使用 add-user-keycloak.sh 添加新用户时如何验证用户的电子邮件?
- android - 错误类型 3 错误:Activity 类 {org.godot.k/com.godot.game.GodotApp} 不存在
- swift - Swift Xcode 无法连接设备
- c# - Blazor 阻止未授权用户的 POST 操作
- playframework - Akka-Stream 2.6.0+ 已弃用的演员系统设置
- mysql - 如何使用临时表更新表以避免插入重复记录?
- javascript - Vuex State 返回值不同
- java - Spring @Value 在嵌套配置中不起作用
- r - 为什么在 R 环境中操作的 netCDF 文件生成的栅格与在 ArcGIS 中创建的栅格具有不同的分辨率?
- vue.js - 如何在 Vue3.0 中使用 Swiper slideTo()?