用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代码绑定即可

相关推荐
又是忙碌的一天2 天前
SpringMVC的处理流程
java·mvc
派大鑫wink2 天前
【Day37】MVC 设计模式:原理与手动实现简易 MVC 框架
java·设计模式·mvc
卓怡学长2 天前
m111基于MVC的舞蹈网站的设计与实现
java·前端·数据库·spring boot·spring·mvc
码界奇点4 天前
基于Spring MVC的缺陷管理系统设计与实现
java·spring·车载系统·毕业设计·mvc·源代码管理
工业甲酰苯胺4 天前
如何一步步将 ASP.NET MVC 升级为.NET
asp.net·mvc·.net
雪人.4 天前
Spring MVC经典面试
java·spring·mvc
我来整一篇6 天前
[Razor] ASP.NET Core MVC 前端组件快速使用总结
前端·asp.net·mvc
lang201509286 天前
Spring MVC文件上传:commons-fileupload解析全流程
java·spring·mvc
岁岁种桃花儿6 天前
Spring Boot核心注解详解:@ResponseBody深度解析与实战
spring boot·spring·mvc
xiaolyuh1239 天前
Spring MVC Bean 参数校验 @Validated
java·spring·mvc