无法使用引导日期选择器

时间:2014-03-04 10:21:30

标签: jquery twitter-bootstrap datepicker bootstrap-datepicker

我试图在输入区域添加Bootstrap date picker,但我无法对其进行配置:

我使用他们的demo page来生成代码并尝试了但似乎我遗漏了一些东西请帮我解决这个问题。

<!DOCTYPE html>
<html>
    <head>
        <title>Bootstrap</title>
        <meta name="viewport" content="width=device-width,initial-scale=1.0">
        <link href="../files/css/bootstrap.min.css" rel="stylesheet">
        <link href="http://eternicode.github.io/bootstrap-datepicker/bootstrap-datepicker/css/datepicker3.css" rel="stylesheet">

        <script src="../files/js/jquery.min.js"></script>
        <script src="../files/js/bootstrap.js"></script>
        <script src="http://eternicode.github.io/bootstrap-datepicker/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
    </head>
    <body>
        <div class="input-group date">
            <input type="text" class="form-control"><span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
        </div>

<script>
    $('#sandbox-container .input-group.date').datepicker({
        format: "yyyy/mm/dd",
        startDate: "2012-01-01",
        endDate: "2015-01-01",
        todayBtn: "linked",
        autoclose: true,
        todayHighlight: true
        });
</script>
    </body>
</html>

我还尝试使用此cssjs的链接:

3 个答案:

答案 0 :(得分:6)

将脚本包含标记移到脚本上方,然后从选择器中删除#sandbox-container - 它不存在。它之后工作(使用正确的URL)...

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script>
<script>
    $('.input-group.date').datepicker({
        format: "yyyy/mm/dd",
        startDate: "2012-01-01",
        endDate: "2015-01-01",
        todayBtn: "linked",
        autoclose: true,
        todayHighlight: true
    });
</script>

<强> jsfiddle example...

答案 1 :(得分:1)

您只需删除#sandbox-container,因为代码中不存在该内容。

试试这个:

<!DOCTYPE html>
<html>
    <head>
        <title>Bootstrap</title>
        <meta name="viewport" content="width=device-width,initial-scale=1.0">
        <link href="../files/css/bootstrap.min.css" rel="stylesheet">
        <link href="http://eternicode.github.io/bootstrap-datepicker/bootstrap-datepicker/css/datepicker3.css" rel="stylesheet">

        <script src="../files/js/jquery.min.js"></script>
        <script src="../files/js/bootstrap.js"></script>
        <script src="http://eternicode.github.io/bootstrap-datepicker/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
    </head>
    <body>
        <div class="input-group date">
            <input type="text" class="form-control"><span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
        </div>

<script>
    $('.input-group.date').datepicker({
        format: "yyyy/mm/dd",
        startDate: "2012-01-01",
        endDate: "2015-01-01",
        todayBtn: "linked",
        autoclose: true,
        todayHighlight: true
    });
</script>
    </body>
</html>

答案 2 :(得分:0)

在调用jQuery,bootstrap.js&amp;之前调用datepicker函数。引导-datepicker.js。如果你调试它,你会得到一个异常,告诉你没有定义“datepicker”。

相反,你应该写:

    <script src="../files/js/jquery.min"></script>
    <script src="../files/js/bootstrap.js"></script>
    <script src="http://eternicode.github.io/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>

    <script>
        $('#sandbox-container .input-group.date').datepicker({
             format: "yyyy/mm/dd",
             startDate: "2012-01-01",
             endDate: "2015-01-01",
             todayBtn: "linked",
             autoclose: true,
             todayHighlight: true
        });
    </script>
相关问题