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

相关推荐
额恩664 天前
神经网络入门:从基础到CNN全解析
mvc
周倦岚5 天前
ArkTS中的MVC、MVP、MVVM
华为·mvc·harmonyos
IKUN家族6 天前
Spring MVC(一)
java·spring·mvc
米码收割机7 天前
【SSM】Spring MVC_MyBatis SSM商城系统(源码+论文)【独一无二】
spring·mvc·mybatis
中微极客10 天前
神经形态计算:从忆阻器到SNN训练工程实践
人工智能·mvc
旧物有情13 天前
游戏开发常用架构 #MVP,MVC
游戏·unity·架构·mvc
sun032216 天前
【笔记】Spring MVC 相关介绍
java·spring·mvc
Qredsun16 天前
RAG 系统 Embedding 流程分析与自托管 API 适配
mvc·embedding
月落星还在20 天前
Spring MVC 与 Spring Boot:从“手动挡”到“自动驾驶”的进化论,兼谈前后端分离的哲学
spring boot·spring·云原生·mvc
wang090724 天前
自己动手写一个spring之MVC_3
java·spring·mvc