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

相关推荐
Cyan_RA92 天前
SpringMVC @RequestMapping的使用演示和细节 详解
java·开发语言·后端·spring·mvc·ssm·springmvc
周杰伦fans5 天前
.net core webapi/mvc阿里云服务器部署 - 错误解决
阿里云·mvc·.netcore
BFT白芙堂6 天前
GRASP 实验室研究 论文解读 | 机器人交互:基于神经网络引导变分推理的快速失配估计
人工智能·神经网络·机器学习·mvc·人机交互·科研教育机器人·具身智能平台
William_cl6 天前
MVC 中 AJAX 与前后端交互深度实战(含独家避坑与场景化方案)
ajax·mvc·交互
Cyan_RA97 天前
SpringMVC 执行流程分析 详解(图解SpringMVC执行流程)
java·人工智能·后端·spring·mvc·ssm·springmvc
hello 早上好7 天前
Spring MVC 类型转换与参数绑定:从架构到实战
spring·架构·mvc
sanggou7 天前
License 集成 Spring Gateway:解决 WebFlux 非阻塞与 Spring MVC Servlet 阻塞兼容问题
spring·gateway·mvc
optimistic_chen8 天前
【Java EE进阶 --- SpringBoot】Spring DI详解
spring boot·笔记·后端·spring·java-ee·mvc·di
速易达网络8 天前
ASP.NET MVC 连接 MySQL 数据库查询示例
数据库·asp.net·mvc
cyforkk9 天前
Spring 异常处理器:从混乱到有序,优雅处理所有异常
java·后端·spring·mvc