最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add DatePicker when I click on text box? - Stack Overflow

programmeradmin2浏览0评论

In my MVC4 razor engine, I need to pick date from text box. It is my View:

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

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

When I click on text box of start date or end date, it should display calendar.

In my MVC4 razor engine, I need to pick date from text box. It is my View:

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

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

When I click on text box of start date or end date, it should display calendar.

Share Improve this question edited Dec 28, 2020 at 1:44 peterh 12.6k20 gold badges89 silver badges113 bronze badges asked Oct 28, 2013 at 12:16 ShuShu 551 gold badge1 silver badge11 bronze badges 3
  • 1 What do you want to do exactly. Do you want to create an MVC helper for this or just use the jQuery DateTimePicker()? – Marco Commented Oct 28, 2013 at 12:19
  • Yes i need datepicker...dont know how to use in my .vbhtml(view page).. – Shu Commented Oct 28, 2013 at 12:24
  • Check this Video, just change the <input/> to @Html.Textbox – AthibaN Commented Oct 28, 2013 at 13:02
Add a comment  | 

5 Answers 5

Reset to default 5

Since you use MVC, jQuery "should" already referenced in your layout page. You will also need the jqueryUI.

If the datepicker code is throwing erros at you, add the following 2 lines, to your view or your layouts page:

<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>

Then you need to declare the elements, which should be get the datepicker functionality.

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

and edit your Html.TextBoxFor() accordingly:

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

This should do the trick. Kind regards

You can use jquery datepicker. DATEPICKER DEMO

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

jsfiddle

<!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();
    });

refer this to your Layout

@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>
发布评论

评论列表(0)

  1. 暂无评论