用JQueryUI库在.net MVC中配置datepicker(时间日期控件)

原文参考:如何在MVC中添加jQuery Datepicker_mvc datepicker-CSDN博客

好文章被埋没了,可能和时间发的早有关。

1.首先我们引入JQuery和JQuery UI

html 复制代码
<!-- ... -->
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>

2.我们给我生成的要用的地方加一个ID,这个参考你们自己就行,主要是给输入框加一个ID

cs 复制代码
      <div class="form-group">
            @Html.Label("入校时间", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.entry_time, new { htmlAttributes = new { @class = "form-control", id = "entry_time" } })
                @Html.ValidationMessageFor(model => model.entry_time, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label("毕业时间", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.graduate_time, new { htmlAttributes = new { @class = "form-control", id = "graduate_time" } })
                @Html.ValidationMessageFor(model => model.graduate_time, "", new { @class = "text-danger" })
            </div>
        </div>

3.导入我们相关的JQuery代码

javascript 复制代码
<script>
    $(document).ready(function () {
        if (!$.fn.datepicker) {
            // 如果 datepicker 还未初始化,尝试加载它
            $.getScript('https://code.jquery.com/ui/1.12.1/jquery-ui.min.js', function () {
                // 确保 datepicker 加载完成后再调用
                $("#entry_time").datepicker();
                $("#graduate_time").datepicker();
            });
        } else {
            // 如果 datepicker 已经初始化,直接调用
            $("#entry_time").datepicker();
            $("#graduate_time").datepicker();
        }
    });


</script>

总结:我们所需要做的就是导入相关的JS和JQuery,用链接导入就行。然后给输入框加一个ID,

然后用JQuery代码绑定即可

相关推荐
极光雨雨1 天前
Java Spring MVC 中 WebMvcConfigurer 和 HandlerInterceptor之间的关系和用法案例
java·spring·mvc
南部余额4 天前
Spring MVC 拦截器interceptor
java·spring·mvc
QQ12958455045 天前
C# 如何能够创建一个MVC的WEB项目
c#·mvc
天若有情6735 天前
Spring MVC文件上传与下载全面详解:从原理到实战
java·spring·mvc·springmvc·javaee·multipart
艾菜籽5 天前
Spring MVC练习:留言板
java·spring·mvc
William_cl5 天前
【C# MVC 前置】异步编程 async/await:从 “卡界面” 到 “秒响应” 的 Action 优化指南(附微软官方避坑清单)
microsoft·c#·mvc
William_cl6 天前
ASP.NET MVC 前置基础:宿主环境 & HttpRuntime 管道,从部署到流程拆透(附避坑指南)
后端·asp.net·mvc
William_cl7 天前
【C# OOP 入门到精通】从基础概念到 MVC 实战(含 SOLID 原则与完整代码)
开发语言·c#·mvc
William_cl9 天前
一、前置基础(MVC学习前提)_核心特性_【C# 泛型入门】为什么说 List<T>是程序员的 “万能收纳盒“?避坑指南在此
学习·c#·mvc
程序员小凯10 天前
Spring MVC 分布式事务与数据一致性教程
分布式·spring·mvc