如何添加日期选择器当我单击文本框

How to add DatePicker when I click on text box?

本文关键字:单击 文本 选择器 日期 何添加 添加      更新时间:2023-09-26

在我的MVC4剃刀引擎中,我需要从文本框中选择日期。我的观点是:

<tr>
  <td>Start Date</td>
  <td>@Html.TextBox("RateListStartDate")</td>
</tr>
<tr>
  <td>End Date</td>
  <td>@Html.TextBox("RateListEndDate")</td>
</tr>

当我单击开始日期或结束日期的文本框时,它应该显示日历。

由于你使用MVC,jQuery"应该"已经在你的布局页面中引用了。您还需要 jqueryUI。

如果日期选取器代码向您抛出错误,请将以下 2 行添加到您的视图或布局页面:

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

然后,您需要声明元素,这些元素应该是获取日期选择器功能。

$(document).ready(function(){
    $(".getdate").each(function() {
        $(this).datepicker();
    });
});

并相应地编辑您的 Html.TextBoxFor():

@Html.TextBox("RateListStartDate", new {@class="getdate"})

这应该可以解决问题。亲切问候

你可以使用 jquery datepicker。日期选择器演示

$(document).ready(function(){
    $(".datepicker").each(function() {
        $(this).datepicker();
    });
});

斯菲德尔

<!doctype html>
  <head>
  <meta charset="utf-8" />
  <title>jQuery UI Datepicker - Restrict date range</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
    $(function() {
      $( "#datepicker" ).datepicker({ minDate: -100, maxDate: "+0D" });
      $("#datepicker").datepicker("setDate",new Date());
      $( "#datepicker" ).datepicker( "option", "dateFormat", "dd/mm/yy");
    });
</script>
</head>
<body>
  <p>Date: <input type="text" id="datepicker" /></p>
</body>
</html>
$(document).ready(function () {
        $('#txtBoxId').datepicker();
    });

请参阅您的布局

@Scripts.Render("~/bundles/jqueryui")
<script>
    $(function()
    {
        $("#date").datepicker();
        $("#date").datepicker
        ({
            dateFormat: "dd-mm-yyyy"
        });
    });
</script>
<tr>
  <td>Start Date</td>
    <div class="form-group">
            @Html.LabelFor(model => model.date, htmlAttributes: new { @class = "control-label   col-md-2" })
            <div class="col-md-10">
                            @Html.EditorFor(model => model.date, new { htmlAttributes = new { @class = "date" } })
                            @Html.ValidationMessageFor(model => model.date, "", new { @class = "text-danger" })
                    </div>
        </div>
</tr>
<tr>
  <td>End Date</td>
    <div class="form-group">
            @Html.LabelFor(model => model.date, htmlAttributes: new { @class = "control-label   col-md-2" })
            <div class="col-md-10">
                            @Html.EditorFor(model => model.date, new { htmlAttributes = new { @class = "date" } })
                            @Html.ValidationMessageFor(model => model.date, "", new { @class = "text-danger" })
                    </div>
        </div>
</tr>