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

javascript - Adding date picker to textbox - Stack Overflow

programmeradmin0浏览0评论

I need to pass two dates to my controller as a filter. I have two text boxes in the View where I need the datepicker to pop up when clicked.

I have tried many variants given on the net. I have tried using Bootstrap Datepicker and JqueryUI. But both doesn't seem to give me any output

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

Scripts

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

Is there any specific place where I should add these scripts. Now i have added the scripts in the head. But I have already tried adding it after the body. I have also added the following at the end of the layout.

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

Is there an issue with the order of the script files?

And my intellisense doesnt show datepicker instead shows datetimepicker.. But i have tried using both with no result. Working with razor.

I need to pass two dates to my controller as a filter. I have two text boxes in the View where I need the datepicker to pop up when clicked.

I have tried many variants given on the net. I have tried using Bootstrap Datepicker and JqueryUI. But both doesn't seem to give me any output

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

Scripts

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

Is there any specific place where I should add these scripts. Now i have added the scripts in the head. But I have already tried adding it after the body. I have also added the following at the end of the layout.

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

Is there an issue with the order of the script files?

And my intellisense doesnt show datepicker instead shows datetimepicker.. But i have tried using both with no result. Working with razor.

Share Improve this question edited Dec 10, 2015 at 13:59 Vini asked Dec 10, 2015 at 13:57 ViniVini 2,1348 gold badges47 silver badges88 bronze badges 13
  • Are you using anything specifically than HTML or CSS? @Scripts.Render() Is it Razor? – Praveen Kumar Purushothaman Commented Dec 10, 2015 at 13:58
  • Yes I am using razor. I am working on a MVC5 application. Added detail in my question – Vini Commented Dec 10, 2015 at 13:59
  • Better to tag it so that Razor users might be able to see this. – Praveen Kumar Purushothaman Commented Dec 10, 2015 at 14:00
  • Can you tell what's happening in the Console? Any errors? – Praveen Kumar Purushothaman Commented Dec 10, 2015 at 14:00
  • i tried setting breakpoints for the script in the console. Its being hit, but i could not find anything that is happening on the button click – Vini Commented Dec 10, 2015 at 14:01
 |  Show 8 more ments

2 Answers 2

Reset to default 3

I added datepicker to your page in this example: http://jsfiddle/zkLc8vpj/

And I am still trying to understand why did you use all those javascript code, u only need to:

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

So i have found that the issue is with the ordering of the script files. this helped me find a solution. I added the script files in the following order in the head of the html file

@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")

and in the bundle config i added the following. The plete bundle file is added to avoid any confusion for a future reader

    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. 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/autoplete.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")); 

    }

I made no changes in the layout though. And the answer has it how to call the date picker.

发布评论

评论列表(0)

  1. 暂无评论