将日期选择器添加到文本框

时间:2015-12-10 13:57:07

标签: javascript jquery twitter-bootstrap razor datepicker

我需要将两个日期作为过滤器传递给我的控制器。我在视图中有两个文本框,我需要点击时弹出日期选择器。

我在网上尝试了很多变种。我尝试过使用Bootstrap Datepicker和JqueryUI。但两者似乎都没有给我任何输出

<th>
     @*<input class="date-picker" />
     <input type="text"  name="from" class="date-picker"/>*@
     <p>
       From : <input type="text" name="from"  id="from"/>
       <br />
       To : <input type="text" name="to" id="to"/>
     </p>
</th>

脚本

<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="jquery-ui-1.8.17.custom.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#from').datepicker();

        $('#from').focus(function () {
            $('#from').datepicker('show');
        });

        $('#from').click(function () {
            $('#from').datepicker('show');
        });
        //$('#ui-datepicker-div').show();
        $('#from').datepicker('show');
    });
</script>

我是否应该添加这些脚本的具体位置。现在我已经在头部添加了脚本。但我已经尝试过将它添加到身体后面。我还在布局的末尾添加了以下内容。

@Scripts.Render("~/bundles/jqueryui")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jqueryajax")
@Scripts.Render("~/bundles/bootstrap") in the layout

脚本文件的顺序是否有问题?

我的intellisense没有显示datepicker而是显示datetimepicker ..但我尝试过使用两者而没有结果。使用剃刀。

2 个答案:

答案 0 :(得分:1)

我在此示例中将datepicker添加到您的页面: http://jsfiddle.net/zkLc8vpj/

我仍然试图理解你为什么使用所有这些javascript代码,你只需要:

$(document).ready(function () {
    $('#from, #to').datepicker();
});

答案 1 :(得分:0)

所以我发现问题在于脚本文件的排序。 this帮助我找到了解决方案。我按照以下顺序在html文件的头部添加了脚本文件

@Styles.Render("~/Content/themes/base/css", "~/Content/css", "~/Content/datepicker3.css")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
@Scripts.Render("~/Content/bootstrap-datepicker.js")

并在捆绑配置中添加了以下内容。添加完整的捆绑文件以避免对未来读者造成任何混淆

    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
            "~/Scripts/jquery-ui-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"));

        // Use the development version of Modernizr to develop with and learn from. Then, when you're
        // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                  "~/Scripts/bootstrap.js",
                  "~/Scripts/respond.js"));

        bundles.Add(new StyleBundle("~/Content/css").Include(
                  "~/Content/bootstrap.css",
                  "~/Content/site.css",
                  "~/Content/Custom.css"));
        bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
      "~/Content/themes/base/core.css",
      "~/Content/themes/base/resizable.css",
      "~/Content/themes/base/selectable.css",
      "~/Content/themes/base/accordion.css",
      "~/Content/themes/base/autocomplete.css",
      "~/Content/themes/base/button.css",
      "~/Content/themes/base/.dialog.css",
      "~/Content/themes/base/slider.css",
      "~/Content/themes/base/tabs.css",
      "~/Content/themes/base/datepicker.css",
      "~/Content/themes/base/progressbar.css",
      "~/Content/themes/base/theme.css")); 

    }

虽然我没有改变布局。 answer有如何调用日期选择器。