【问题标题】:Bootstrap 3 Datepicker doesn't show calendarBootstrap 3 Datepicker 不显示日历
【发布时间】:2015-08-27 05:17:18
【问题描述】:

我的页面中包含以下内容:

在<head>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<!-- Optional theme -->
<!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">-->

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>

<!-- Latest compiled and minified jQuery Core -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

&lt;body&gt; 的底部

<!-- Moments.js minified CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>

<!-- Bootstrap datepicker -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/js/bootstrap-datepicker.min.js"></script>

然后我使用 jQuery 将表单的输入字段转换为日期选择器:

<div class="form-group">
    <div class='input-group date' id='datetimepicker1'>
        <input type='text' class="form-control" />
        <span class="input-group-addon">
            <span class="glyphicon glyphicon-calendar"></span>
        </span>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        $('#datetimepicker1').datetimepicker();
    });
</script>

输入字段的样式类似于日期选择器(末尾带有日历图标的字段),但是当我单击该字段或图标上的任意位置时,日历不会显示。

我重新阅读了Installation 教程,但我没有发现任何遗漏。

【问题讨论】:

  • 在 bootstrap.min.js 之前加载 jquery.min.js

标签: javascript jquery twitter-bootstrap-3 datepicker calendar


【解决方案1】:

替换

  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/js/bootstrap-datepicker.min.js"></script>

由

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.14.30/js/bootstrap-datetimepicker.min.js"></script>

您应该在浏览器控制台中获得TypeError: $(...).datetimepicker is not a function。

因为您只包含 datepicker 而不是 datetimepicker。

Live Demo

【讨论】:

  • 还必须为 datetime-picker 添加 CSS,否则日历看起来像垃圾。
【解决方案2】:

添加datetimepicker1到&lt;input type='text' class="form-control" id="datetimepicker1" /&gt;

和$('#datetimepicker1').datepicker(); 不是$('#datetimepicker1').datetimepicker();

在这里工作:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <!-- Optional theme -->
    <!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">-->
</head>
<body>


<div class="form-group">
    <div class='input-group date' >
        <input type='text' class="form-control" id="datetimepicker1" />
        <span class="input-group-addon">
            <span class="glyphicon glyphicon-calendar"></span>
        </span>
    </div>
</div>



<!-- Latest compiled and minified jQuery Core -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/js/bootstrap-datepicker.min.js"></script>
<script type="text/javascript">
    $(function () {
        $('#datetimepicker1').datepicker();
    });
</script>
</body>
</html>

【讨论】:

    【解决方案3】:

    如果你打开浏览器控制台,你会在控制台看到异常错误,比如bootstrap JS requires jquery左右

    <!-- Latest compiled and minified jQuery Core -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js">    </script>
    

    这应该可以工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多